' We have to call the Win32 API (see Direct I/O online help)
' Declare the neccessary functions and constants
Private Declare Function CreateFile Lib "KERNEL32" Alias "CreateFileA" ( _
ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const FILE_SHARE_READ As Long = 1
Private Const FILE_SHARE_WRITE As Long = 2
Private Const OPEN_EXISTING As Long = 3
Private Const INVALID_HANDLE_VALUE As Long = -1
' Unfortunately (?!?) VB doesn't provide functions for hardware access.
' So we have to take a 3rd party supplement (there are many freeware libs around,
' search the web for Win95io.zip or inpout32.zip).
' The following code is taken from Win95io:
Private Declare Sub vbOut Lib "WIN95IO.DLL" ( _
ByVal nPort As Integer, _
ByVal nData As Integer)
Private Declare Sub vbOutw Lib "WIN95IO.DLL" ( _
ByVal nPort As Integer, _
ByVal nData As Integer)
Private Declare Function vbInp Lib "WIN95IO.DLL" ( _
ByVal nPort As Integer) As Integer
Private Declare Function vbInpw Lib "WIN95IO.DLL" ( _
ByVal nPort As Integer) As Integer
Private Sub Form_Load()
Dim hDevice As Long
hDevice = CreateFile("\\.\DirectIo0", GENERIC_READ Or GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)
If hDevice = INVALID_HANDLE_VALUE Then
' ask what went wrong
MsgBox "Unable to open driver (error code = " & Err.LastDllError & _
"). Aborting..."
' terminate (it is useless to continue if we can’t connect to Direct I/O)
End
End If
End Sub
Private Sub Read_Click()
Value = vbInp(Port)
End Sub
Private Sub Write_Click()
vbOut Port, Value
End Sub