请高手帮忙解答一下,调用SetupDiGetDeviceInterfaceDetail出现错误,USB通信时出现的问题
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)
--第二遍调用说提供给请求操作的用户缓冲区无效
下面是其中结构体的定义以及函数的调用
<StructLayout(LayoutKind.Sequential)>
Public Structure SP_DEVICE_INTERFACE_DATA
Public cbSize As UInteger
Public interfaceClassGuid As Guid
Public flags As UInteger
Public reserved As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)>
Public Structure SP_DEVINFO_DATA
Public cbSize As Integer
Public classGuid As Guid
Public propertyId As Integer
Public reserved As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)>
Public Structure SP_DEVICE_INTERFACE_DETAIL_DATA
Public cbSize As UInt32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> Public DevicePath As String
End Structure
--函数
<DllImport("setupapi.dll", EntryPoint:="SetupDiGetClassDevsW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, PreserveSig:=True, CallingConvention:=CallingConvention.Winapi)>
Private Shared Function SetupDiGetClassDevs(
ByRef ClassGuid As Guid,
ByVal Enumerator As Integer,
ByVal hwndParent As Integer,
ByVal Flags As Integer) As IntPtr
End Function
<DllImport("setupapi.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
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
[此贴子已经被作者于2017-3-29 11:48编辑过]