首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

USB HID通讯中,在CreatFile返回总是-1是什么有关问题呀

2012-02-17 
USB HID通讯中,在CreatFile返回总是-1是什么问题呀?PrivateSubGetHidGuid_Click()DimResultAsLongDimMembe

USB HID通讯中,在CreatFile返回总是-1是什么问题呀?
Private   Sub   GetHidGuid_Click()
Dim   Result   As   Long
Dim   MemberIndex   As   Long
       
'/*   Get   GUID   for   all   System   HIDs   */
        Result   =   HidD_GetHidGuid(HidGuid)

'/*   Get   Device   Information   for   all   present   devices   */
        DeviceInfoSet   =   SetupDiGetClassDevs(   _
                                                                        HidGuid,   _
                                                                        vbNullString,   _
                                                                        0,   _
                                                                        (DIGCF_PRESENT   Or   DIGCF_DEVICEINTERFACE))
       
'/*   Scan   all   Devices   */           查找设备
        MemberIndex   =   0
        ADevice.Total   =   0
        Do
       
                MyDeviceInterfaceData.cbSize   =   LenB(MyDeviceInterfaceData)
'/*   Device   Interface   Element   of   a   Device   Information   set   */
                Result   =   SetupDiEnumDeviceInterfaces(   _
                                                                                                DeviceInfoSet,   _
                                                                                                0,   _
                                                                                                HidGuid,   _
                                                                                                MemberIndex,   _


                                                                                                MyDeviceInterfaceData)
                MemberIndex   =   MemberIndex   +   1
                If   Result   =   0   Then   Exit   Do

'/*   Get   Device   Interface   Details   -   Get   Length   */
'第一次调用函数DeviceInfoSet,MyDeviceInterfaceData是传回结构
'Needed包含要传给下一个调用的缓冲区大小
                Result   =   SetupDiGetDeviceInterfaceDetail(   _
                                                                                                DeviceInfoSet,   _
                                                                                                MyDeviceInterfaceData,   _
                                                                                                0,   _
                                                                                                0,   _
                                                                                                Needed,   _
                                                                                                0)
                                                                                       
'要传给下一个调用的   DetailData=Needed
                DetailData   =   Needed
                MyDeviceInterfaceDetailData.cbSize   =   Len(MyDeviceInterfaceDetailData)


'Use   a   byte   array   to   allocate   memory   for
'the   MyDeviceInterfaceDetailData   structure
                ReDim   DetailDataBuffer(Needed)
'字节数组的前4个字节是数组的大小
                Call   RtlMoveMemory(DetailDataBuffer(0),   MyDeviceInterfaceDetailData,   4)
'VarPtr(DetailDataBuffer(0))是包含MyDeviceInterfaceDetailData的字节数组的开始地址
'DetailData储存上一个调用所传回的大小
                Result   =   SetupDiGetDeviceInterfaceDetail(   _
                                                                                                DeviceInfoSet,   _
                                                                                                MyDeviceInterfaceData,   _
                                                                                                VarPtr(DetailDataBuffer(0)),   _
                                                                                                DetailData,   _
                                                                                                Needed,   _
                                                                                                0)

                                                                                               
                If   Result   =   0   Then   GoTo   DO_Next
               
               
                DevicePathName   =   CStr(DetailDataBuffer())
                'Convert   to   Unicode.


                DevicePathName   =   StrConv(DevicePathName,   vbUnicode)
                'Strip   cbSize   (4   bytes)   from   the   beginning.
                DevicePathName   =   Right$(DevicePathName,   Len(DevicePathName)   -   4)
               
               
                HidDevice   =   CreateFile(   _
                                                                        DevicePathName,   _
                                                                        GENERIC_READ   Or   GENERIC_WRITE,   _
                                                                        (FILE_SHARE_READ   Or   FILE_SHARE_WRITE),   _
                                                                        0,   _
                                                                        OPEN_EXISTING,   _
                                                                        0,   _
                                                                        0)
这里总是失败什么问题呀?
                If   HidDevice   =   INVALID_HANDLE_VALUE   Then   GoTo   DO_Next
               
                Result   =   HidD_GetPreparsedData(   _
                                                                                        HidDevice,   _
                                                                                        PreparsedData)


                If   Result   =   0   Then   GoTo   DO_Next
                Result   =   HidP_GetCaps(   _
                                                                PreparsedData,   _
                                                                Capabilities)
                If   (Capabilities.UsagePage   =   &HFF00)   And   (Capabilities.Usage   =   &H1)   Then
                        ADevice.DevicePath(ADevice.Total)   =   DevicePathName
                        ADevice.Total   =   ADevice.Total   +   1
                End   If
DO_Next:
               
        Loop   Until   NumMax   <   ADevice.Total
       
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 Security_Attributes, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) _
As Long

热点排行