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

解决方案 »

  1.   

    HidDevice = CreateFile( _
                                        DevicePathName, _
                                        GENERIC_READ Or GENERIC_WRITE, _
                                        (FILE_SHARE_READ Or FILE_SHARE_WRITE), _
                                        0, _
                                        OPEN_EXISTING, _
                                        0, _
                                        0)
    DevicePathName与VC出来的是一样,VC打开正常,但VB不行????
      

  2.   

    DevicePathName变量值是如下:\\?\hid#vid_c251&pid_1301#6&18b59850&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030} 为什么返回值总是-1
      

  3.   

    自己用VC写DLL,VC调用成功,但用VB也不行,到底是什么问题呀?
      

  4.   

    请注意第四个函数原型: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