VB源代码
为什么不能获得USB设备句柄
我用VB写的应用程序,用的是68013芯片,驱动程序也已经安装好,并且操作系统已经能识别设备,可是应用程序用creatfile就是打不开设备,得不到设备句柄。代码如下:
Private Sub Form_Load()
'Handle HidDevice
Dim HidDevice As Long
DevicePathName = "\\.\ezusb-0"
HidDevice = CreateFile _
(DevicePathName, _
GENERIC_WRITE, _
FILE_SHARE_WRITE, _
0, _
OPEN_EXISTING, _
0, _
0)
'INVALID_HANDLE_VALUE = -1
If (HidDevice = INVALID_HANDLE_VALUE) Then
'OpenDevice = False
'Form1.Shape6.Height = (MaxTemper - 0) * TemperScale
MsgBox " 未发现与之通信的USB设备!" + _
Chr(13) & Chr(10) + _
Chr(13) & Chr(10) + _
" 请确定其硬件设备是否已连接," + _
Chr(13) & Chr(10) + _
"并确定其驱动程序是否已加载。", _
17, "可恶"
Else
MsgBox " 发现与之通信的USB设备!" + _
Chr(13) & Chr(10) + _
Chr(13) & Chr(10) + _
" 请确定," + _
Chr(13) & Chr(10) + _
"!", _
1, "成功打开设备"
'OpenDevice = True
End If
End Sub
Public Declare Function CreateFile _
Lib "kernel32" _
Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByRef lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) _
As Long
'*****************************************************************
'API constants.
'*****************************************************************
'IOCTL Code
'Public Const FILE_DEVICE_UNKNOWN = &H22
Public Const Cyusb_IOCTL_INDEX = &H800
Public Const METHOD_BUFFERED = &H0
Public Const FILE_ANY_ACCESS = &H0
'CreateFile
Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const FILE_SHARE_READ = &H1
Public Const FILE_SHARE_WRITE = &H2
Public Const OPEN_EXISTING = 3
'Public Const INVALID_HANDLE_VALUE = -1
Public Const INVALID_HANDLE_VALUE = -1