问题描述
公共 Hi, Public 模块 HIDModule Module HIDModule 公共 子 DetectDevice() Public Sub DetectDevice() 昏暗 HidGuid 如 GUID Dim HidGuid As GUID 昏暗 DeviceInfoSet 如 长 Dim DeviceInfoSet As Long 昏暗 结果 如 长 Dim Result As Long 昏暗 MemberIndex 如 长 Dim MemberIndex As Long 昏暗 MyDeviceInterfaceData 如 SP_DEVICE_INTERFACE_DATA Dim MyDeviceInterfaceData As SP_DEVICE_INTERFACE_DATA 昏暗 LastDevice 如 布尔值 Dim LastDevice As Boolean 昏暗 RequiredSize 如 长 Dim RequiredSize As Long 昏暗 DumRequiredSize 如 长 Dim DumRequiredSize As Long 昏暗 MyDeviceInterfaceDetailData 如 SP_DEVICE_INTERFACE_DETAIL_DATA Dim MyDeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA 昏暗 DetailDataBuffer() 如 字节 Dim DetailDataBuffer() As Byte 昏暗 DevicePathName 如 字符串 Dim DevicePathName As String 致电 HidD_GetHidGuid(HidGuid) DeviceInfoSet = SetupDiGetClassDevs(HidGuid,vbNullString,0,(DIGCF_PRESENT Call HidD_GetHidGuid(HidGuid) DeviceInfoSet = SetupDiGetClassDevs(HidGuid, vbNullString, 0, (DIGCF_PRESENT 或 DIGCF_DEVICEINTERFACE)) Or DIGCF_DEVICEINTERFACE)) 如果 DeviceInfoSet = INVALID_HANDLE_VALUE 然后 If DeviceInfoSet = INVALID_HANDLE_VALUE Then 退出 子 Exit Sub 结束 如果 End If LastDevice = LastDevice = 错误 MemberIndex = 0 MemberIndex = 0 要做 MyDeviceInterfaceData.cbSize = Len(MyDeviceInterfaceData) MyDeviceInterfaceData.cbSize = Len(MyDeviceInterfaceData) 结果= SetupDiEnumDeviceInterfaces(DeviceInfoSet,0,HidGuid,MemberIndex,MyDeviceInterfaceData) Result = SetupDiEnumDeviceInterfaces(DeviceInfoSet, 0, HidGuid, MemberIndex, MyDeviceInterfaceData) 如果 结果= 0 然后 If Result = 0 Then LastDevice = LastDevice = 真实 其他 结果= SetupDiGetDeviceInterfaceDetail(DeviceInfoSet,MyDeviceInterfaceData,0,_ Result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, MyDeviceInterfaceData, 0, _ 0,RequiredSize,0) 0, RequiredSize, 0) 如果 结果= 0 和 Err.LastDllError = ERROR_INSUFFICIENT_BUFFER 然后 If Result = 0 And Err.LastDllError = ERROR_INSUFFICIENT_BUFFER Then MyDeviceInterfaceDetailData.cbSize = Len(MyDeviceInterfaceDetailData) MyDeviceInterfaceDetailData.cbSize = Len(MyDeviceInterfaceDetailData) ReDim DetailDataBuffer(RequiredSize) ReDim DetailDataBuffer(RequiredSize) 致电 RtlMoveMemory(DetailDataBuffer(0),MyDeviceInterfaceDetailData,4) 结果= SetupDiGetDeviceInterfaceDetail(DeviceInfoSet,MyDeviceInterfaceData,_ Call RtlMoveMemory(DetailDataBuffer(0), MyDeviceInterfaceDetailData, 4) Result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, MyDeviceInterfaceData, _ VarPtr(DetailDataBuffer(0)),RequiredSize,_ VarPtr(DetailDataBuffer(0)), RequiredSize, _ DumRequiredSize,0) DumRequiredSize, 0) DevicePathName = DevicePathName = CStr (DetailDataBuffer(0)) DevicePathName = StrConv(DevicePathName,VbStrConv.None) CStr(DetailDataBuffer(0)) DevicePathName = StrConv(DevicePathName, VbStrConv.None) DevicePathName = Right $(DevicePathName,Len(DevicePathName)-4) DevicePathName = Right$(DevicePathName, Len(DevicePathName) - 4) Security.lpSecurityDescriptor = 0 Security.lpSecurityDescriptor = 0 Security.bInheritHandle = Security.bInheritHandle = 真实 Security.nLength = Len(安全性) Security.nLength = Len(Security) HidDevice = CreateFile(DevicePathName,GENERIC_READ HidDevice = CreateFile(DevicePathName, GENERIC_READ 或 GENERIC_WRITE,_ (FILE_SHARE_READ Or GENERIC_WRITE, _ (FILE_SHARE_READ 或 FILE_SHARE_WRITE),安全性,_ OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0) Or FILE_SHARE_WRITE), Security, _ OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0) 如果 HidDevice = INVALID_HANDLE_VALUE 然后 If HidDevice = INVALID_HANDLE_VALUE Then HidDevice = CreateFile(DevicePathName,GENERIC_NOACCESS,_ HidDevice = CreateFile(DevicePathName, GENERIC_NOACCESS, _ (FILE_SHARE_READ (FILE_SHARE_READ 或 FILE_SHARE_WRITE),_ 安全性,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0) Or FILE_SHARE_WRITE), _ Security, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0) 结束 如果 End If 如果 HidDevice<> INVALID_HANDLE_VALUE 然后 跨度> DeviceAttributes.Size = Len(DeviceAttributes) DeviceAttributes.Size = Len(DeviceAttributes) 如果 HidD_GetAttributes(HidDevice,DeviceAttributes) 然后 If HidD_GetAttributes(HidDevice, DeviceAttributes) Then 如果 ((DeviceAttributes.VendorID = IdtVendorID) 或 _ (DeviceAttributes.VendorID = MagTekVendorID) If ((DeviceAttributes.VendorID = IdtVendorID) Or _ (DeviceAttributes.VendorID = MagTekVendorID) 或 _ (DeviceAttributes.VendorID = HpVendorID)) Or _ (DeviceAttributes.VendorID = HpVendorID)) 并且 _ (((DeviceAttributes.ProductID = InsertRdrProdID) And _ ((DeviceAttributes.ProductID = InsertRdrProdID) 或 _ (DeviceAttributes.ProductID = InsertRdrProdMagID) Or _ (DeviceAttributes.ProductID = InsertRdrProdMagID) 或 _ (DeviceAttributes.ProductID = InsertRdrProdID2) Or _ (DeviceAttributes.ProductID = InsertRdrProdID2) 或 _ (DeviceAttributes.ProductID = MagTekProductID) Or _ (DeviceAttributes.ProductID = MagTekProductID) 或 _ (DeviceAttributes.ProductID = MagTekProductID2) Or _ (DeviceAttributes.ProductID = MagTekProductID2) 或 _ (DeviceAttributes.ProductID = HpProductID)) Or _ (DeviceAttributes.ProductID = HpProductID)) 然后 如果 HidD_GetPreparsedData(HidDevice,PreparsedData) 然后 If HidD_GetPreparsedData(HidDevice, PreparsedData) Then 结果= HidP_GetCaps(PreparsedData,功能) Result = HidP_GetCaps(PreparsedData, Capabilities) 如果 结果<> HIDP_STATUS_SUCCESS 然后 跨度> CloseDevice() CloseDevice() 结束 如果 End If 其他 CloseDevice() CloseDevice() 结束 如果 End If 其他 CloseDevice() CloseDevice() 结束 如果 End If 其他 CloseDevice() CloseDevice() 结束 如果 End If 结束 如果 End If 结束 如果 End If 结束 如果 End If MemberIndex = MemberIndex + 1 MemberIndex = MemberIndex + 1 循环 直到 (LastDevice = 真实 ) 或 (HidDevice<> INVALID_HANDLE_VALUE) 结果= SetupDiDestroyDeviceInfoList(DeviceInfoSet) Loop Until (LastDevice = True) Or (HidDevice <> INVALID_HANDLE_VALUE) Result = SetupDiDestroyDeviceInfoList(DeviceInfoSet) 结束 Sub 如果 结果= 0 和 Err.LastDllError = ERROR_INSUFFICIENT_BUFFER 然后 RtlMoveMemory方法.由于相同的代码在vb中可以正常工作,因此应该与.net限制有关. End Sub If Result = 0 And Err.LastDllError = ERROR_INSUFFICIENT_BUFFER Then RtlMoveMemory method.Since the same code works perfectly in vb it should be something related to .net limitation.Please help. 这篇关于vb.net中的USB读卡器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
我想创建一个可以检测USB读卡器设备的应用程序,当我刷卡时应该可以读取.下面的代码在VB中可以正常工作.但是当我将其转换为vb.net出现了一些问题.下面是代码.如果有人可以帮助我,那将非常好.
I want to create an application that can detect usb card reader device and when i swipe the card it should read.The below code WORKS PERFECTLY IN VB.But when i converted it to vb.net it is showing some problem.Below is the code for that.It would be great if anybody can help me.
这种方法会陷入无限循环,如果我删除条件,
然后在
This method is going in an infinite loop and if i remove the condition
then am getting exception in 推荐答案