本文介绍了vb.net中的USB读卡器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想创建一个可以检测USB读卡器设备的应用程序,当我刷卡时应该可以读取.下面的代码在VB中可以正常工作.但是当我将其转换为vb.net出现了一些问题.下面是代码.如果有人可以帮助我,那将非常好.

公共

Hi,

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.

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
This method is going in an infinite loop and if i remove the condition

If Result = 0 And Err.LastDllError = ERROR_INSUFFICIENT_BUFFER Then
then am getting exception in

RtlMoveMemory method.Since the same code works perfectly in vb it should be something related to .net limitation.Please help.

推荐答案


这篇关于vb.net中的USB读卡器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 18:17