调用SetupDiEnumDeviceInterfaces枚举USB设备失败
我最近在做一个操作USB设备的程序,但在打开设备时遇到了问题.步骤如下,
1.调用SetupDiGetClassDevs.这一部正常获得句柄
2.调用SetupDiEnumDeviceInterfaces,枚举设备.在调用时无论如何返回值也是false.
3.两次调用SetupDiGetDeviceInterfaceDetail,获取设备详细信息,设备名.
4.调用CreateFile,打开设备句柄.
这里3和4因为步骤2不能正确获取信息,而不能进行.
因为这段代码在VC下运行正常,所以我怀疑是我在vb.net下的调用不对.
代码如下
//函数原型
WINSETUPAPI
BOOL
WINAPI
SetupDiEnumDeviceInterfaces(
__in HDEVINFO DeviceInfoSet,
__in_opt PSP_DEVINFO_DATA DeviceInfoData,
__in CONST GUID *InterfaceClassGuid,
__in DWORD MemberIndex,
__out PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData
);
'''vb.net中的调用
<DllImport("setupapi.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function SetupDiEnumDeviceInterfaces(ByVal hDevInfo As IntPtr, _
ByVal devInfo As IntPtr, ByRef interfaceClassGuid As Guid, ByVal memberIndex As UInt32, _
ByRef deviceInterfaceData As SP_DEVICE_INTERFACE_DATA) As Boolean
End Function
'''SP_DEVICE_INTERFACE_DATA 结构体
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Public Class SP_DEVICE_INTERFACE_DATA
Public cbSize As Integer
Public classGuid As Guid
Public devInst As Integer
Public reserved As Integer
End Class
希望有经验的朋友可以帮忙看一下,问题出在哪里
[解决办法]
C#
http://www.codeproject.com/KB/system/usbeject.aspx
http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/b772573e-c78d-4032-85eb-7e734ccebaff
http://hidlibrary.codeplex.com/