同标题,我用vb写一个和USB的HID自定义设备通信的程序,在VB5编译器中能正常收发,可是生成exe后则无法正确执行,经检查是createfile返回失败,这是为什么呢?哪位大侠能告诉我,不胜感激涕零。

解决方案 »

  1.   


    '补充:相关代码如下:    DeviceInfoSet = SetupDiGetClassDevs(HidGuid, vbNullString, 0, DIGCF_PRESENT Or DIGCF_INTERFACRDEVICE)
        If DeviceInfoSet = INVALID_HANDLE_VALUE Then
            Text1.Text = Text1.Text & "查找USB设备类失败!" & vbCrLf
            Exit Sub
        End If
        
        MyDeviceInterfaceData.cbSize = LenB(MyDeviceInterfaceData)
        For MemberIndex = 0 To 9
            Result = SetupDiEnumDeviceInterfaces(DeviceInfoSet, 0, HidGuid, MemberIndex, MyDeviceInterfaceData)
            If Result = 0 Then
                Text1.Text = Text1.Text & "查找USB设备数据结构失败!" & vbCrLf
                Exit Sub
            End If
            Result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, MyDeviceInterfaceData, 0, 0, Needed, 0)
            DetailData = Needed
            MyDeviceInterfaceDetailData.cbSize = Len(MyDeviceInterfaceDetailData)
            ReDim DetailDataBuffer(Needed)
            Call RtlMoveMemory(DetailDataBuffer(0), MyDeviceInterfaceDetailData, 4)
            Result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, MyDeviceInterfaceData, VarPtr(DetailDataBuffer(0)), DetailData, Needed, 0)
            If Result = 0 Then
                Text1.Text = Text1.Text & "查找设备路径时出错!" & vbCrLf
            Else
                DevicePathName = CStr(DetailDataBuffer())
                DevicePathName = StrConv(DevicePathName, vbUnicode)
                DevicePathName = Right(DevicePathName, Len(DevicePathName) - 4)
                'Text1.Text = Text1.Text & DevicePathName & vbCrLf
               ' HidDevice = CreateFile(DevicePathName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
                HidDevice = CreateFile(DevicePathName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0)
                If HidDevice <> INVALID_HANDLE_VALUE Then
                    DeviceAttributes.Size = LenB(DeviceAttributes)
                    Result = HidD_GetAttributes(HidDevice, DeviceAttributes)
                    If DeviceAttributes.ProductID = MyProductID And DeviceAttributes.VendorID = MyVendorID Then
                        Text1.Text = Text1.Text & "经过识别PID与VID,,找到了正确的设备!成功了!" & vbCrLf
                    End If
                    Result = HidD_GetPreparsedData(HidDevice, PreparsedData)
                    Result = HidP_GetCaps(PreparsedData, Capabilities)
                    InputReportByteLength = Capabilities.InputReportByteLength
                    OutputReportByteLength = Capabilities.OutputReportByteLength
                    ReDim ReadBuffer(InputReportByteLength - 1)
                    ReDim SendBuffer(OutputReportByteLength - 1)
                    Timer1.Enabled = True
                    Exit For
                Else
                    Text1.Text = Text1.Text & "第" & (MemberIndex + 1) & "个设备无法打开!" & vbCrLf
                End If
            End If
        Next MemberIndex
        
        Exit Sub
    errordeal:
        Text1.Text = Text1.Text & "未找到相应的USB设备或设备打开失败,请检查!" & vbCrLf
      

  2.   

    额,先谢谢楼上回复,可关键是我在编译器里就完全成功了啊,只是生成exe后就不行了。。好无奈
      

  3.   

    检查一下生成的exe文件格式是否正确?如OEP等