作者:aloneloveu2005 | 来源:互联网 | 2024-11-29 19:40
HidD_GetHidGuid(guidHID)hDevInfoSetupDiGetClassDevs(guidHID,0,0,DIGCF.DIGCF_PRESENTOrDIGCF
HidD_GetHidGuid(guidHID)
hDevInfo = SetupDiGetClassDevs(guidHID, 0, 0, DIGCF.DIGCF_PRESENT Or DIGCF.DIGCF_DEVICEINTERFACE)
DeviceInterfaceData.cbSize = Marshal.SizeOf(GetType(SP_DEVICE_INTERFACE_DATA))
DetailData.cbSize = Marshal.SizeOf(GetType(SP_DEVICE_INTERFACE_DETAIL_DATA))
strtInterfaceData.cbSize = Marshal.SizeOf(GetType(SP_DEVINFO_DATA))
result = SetupDiEnumDeviceInterfaces(hDevInfo, 0, guidHID, MemberIndex, DeviceInterfaceData)
--这之前的运行都是没有问题的
result = SetupDiGetDeviceInterfaceDetail(hDevInfo, DeviceInterfaceData, DetailData, 0, bufferSize, strtInterfaceData)
--第一遍调用说成功
result = SetupDiGetDeviceInterfaceDetail(hDevInfo, DeviceInterfaceData, DetailData, bufferSize, bufferSize, strtInterfaceData)
--第二遍调用说提供给请求操作的用户缓冲区无效
下面是其中结构体的定义以及函数的调用
Public Structure SP_DEVICE_INTERFACE_DATA
Public cbSize As UInteger
Public interfaceClassGuid As Guid
Public flags As UInteger
Public reserved As IntPtr
End Structure
Public Structure SP_DEVINFO_DATA
Public cbSize As Integer
Public classGuid As Guid
Public propertyId As Integer
Public reserved As IntPtr
End Structure
Public Structure SP_DEVICE_INTERFACE_DETAIL_DATA
Public cbSize As UInt32
Public DevicePath As String
End Structure
--函数
Private Shared Function SetupDiGetClassDevs(
ByRef ClassGuid As Guid,
ByVal Enumerator As Integer,
ByVal hwndParent As Integer,
ByVal Flags As Integer) As IntPtr
End Function
Public Shared Function SetupDiEnumDeviceInterfaces(
ByVal hDevInfo As IntPtr,
ByVal devInfo As Integer,
ByRef interfaceClassGuid As Guid,
ByVal memberIndex As UInt32,
ByRef deviceInterfaceData As SP_DEVICE_INTERFACE_DATA) As Boolean
End Function
Public Declare Auto Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" Alias "SetupDiGetDeviceInterfaceDetailW" (
ByVal hDevInfo As IntPtr,
ByRef deviceInterfaceData As SP_DEVICE_INTERFACE_DATA,
ByRef deviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA,
ByVal deviceInterfaceDetailDataSize As Int32,
ByRef requiredSize As Int32,
ByRef deviceInfoData As SP_DEVINFO_DATA) As Boolean
6 个解决方案
你看下第一次调用后 bufferSize是多少
还有DetailData.cbSize的值是多少
这些值出现异常都是读不到的
detaildata.cbsize 是260
buffersize是0
Public Declare Auto Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" Alias "SetupDiGetDeviceInterfaceDetailW" _
(ByVal hDevInfo As IntPtr,
ByRef deviceInterfaceData As SP_DEVICE_INTERFACE_DATA,
ByvaldeviceInterfaceDetailData As IntPtr,
ByVal deviceInterfaceDetailDataSize As Int32,
ByRef requiredSize As Int32,
ByVal deviceInfoData As IntPtr) As Boolean
我把函数定义改成这样,然后引用
result = SetupDiGetDeviceInterfaceDetail(hDevInfo, DeviceInterfaceData, IntPtr.Zero, 0, bufferSize, IntPtr.Zero)
DetailData.cbSize = Marshal.SizeOf(GetType(SP_DEVICE_INTERFACE_DETAIL_DATA))
strtInterfaceData.cbSize = Marshal.SizeOf(GetType(SP_DEVINFO_DATA))
detailDataBuffer = Marshal.AllocHGlobal(bufferSize)
Marshal.StructureToPtr(DetailData, detailDataBuffer, False)
result = SetupDiGetDeviceInterfaceDetail(hDevInfo, DeviceInterfaceData, detailDataBuffer, bufferSize, bufferSize, IntPtr.Zero)
这样显示buffersize192,但是第二遍调用后的错误还是一样的
问题找到了 需要这样
If (IntPtr.Size = 8) Then
DetailData.cbSize = 8
Else
DetailData.cbSize = 4 + Marshal.SystemDefaultCharSize
End If
marshal.sizeof的问题有点多啊说实话