私はこれにしばらく苦労しており、これを機能させるためにあらゆる種類のことを試みています。初めて SetupDiGetDeviceInterfaceDetail を呼び出す唯一の目的は RequiredSize を正しい値に設定することであるため、 SetupDiGetDeviceInterfaceDetail は初めて 1784 エラーを発生させるはずだと私は理解しています。2 回目に呼び出されると、実際に機能し、有効な DeviceInterfaceDetailData 構造が得られるはずです。2回目の呼び出しでこれが失敗する原因はわかりません。
構造定義:
<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
Public Structure SP_DEVICE_INTERFACE_DATA
Public cbSize As Integer
Public InterfaceClassGuid As System.Guid
Public Flags As Integer
Public Reserved As UIntPtr
End Structure
Public Structure SP_DEVINFO_DATA
Public cbSize As Integer
Public ClassGuid As System.Guid
Public DevInst As Integer
Public Reserved As UIntPtr
End Structure
関数宣言:
Public Declare Auto Function SetupDiGetClassDevs Lib "setupapi.dll" (ByRef ClassGuid As System.Guid, ByVal Enumerator As Integer, ByVal hwndParent As IntPtr, ByVal Flags As Integer) As IntPtr
Public Declare Auto Function SetupDiEnumDeviceInfo Lib "setupapi.dll" (ByVal DeviceInfoSet As Integer, ByVal MemberIndex As Integer, ByRef DeviceInfoData As SP_DEVINFO_DATA) As Boolean
Public Declare Auto Function SetupDiEnumDeviceInterfaces Lib "setupapi.dll" (ByVal DeviceInfoSet As IntPtr, ByRef DeviceInfoData As SP_DEVINFO_DATA, ByRef InterfaceClassGuid As System.Guid, ByVal MemberIndex As UInteger, ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA) As Boolean
//For the first call
Public Declare Auto Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" (ByVal DeviceInfoSet As IntPtr, ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, ByVal DeviceInterfaceDetailData As IntPtr, ByVal DeviceInterfaceDetailDataSize As Integer, ByRef RequiredSize As Integer, ByRef DeviceInfoData As IntPtr) As Boolean
//For the second call
Public Declare Auto Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" (ByVal DeviceInfoSet As IntPtr, ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, ByRef DeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA, ByVal DeviceInterfaceDetailDataSize As Integer, ByRef RequiredSize As Integer, ByRef DeviceInfoData As SP_DEVINFO_DATA) As Boolean
関数呼び出し:
Dim DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA
Dim DeviceInterfaceDetailDataSize As Integer
Dim DeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA = Nothing
Dim DeviceInfoSet As IntPtr
Dim RequiredSize As Integer
Dim HidGuid As System.Guid
HidD_GetHidGuid(HidGuid)
Dim LastError As Integer
DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData)
DeviceInterfaceDetailData.cbSize = Marshal.SizeOf(DeviceInterfaceDetailData
DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData)
DeviceInfoSet = SetupDiGetClassDevs(HidGuid, 0, IntPtr.Zero, DIGCF_PRESENT + DIGCF_DEVICEINTERFACE)
success = SetupDiEnumDeviceInfo(DeviceInfoSet, 0, DeviceInfoData)
LastError = GetLastError()
success = SetupDiEnumDeviceInterfaces(DeviceInfoSet, DeviceInfoData, HidGuid, 0, DeviceInterfaceData)
success = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, DeviceInterfaceData, IntPtr.Zero, 0, RequiredSize, IntPtr.Zero)
//Success is false. GetLastError() shows 1784, but RequiredSize gets set to 166
DeviceInterfaceDetailDataSize = RequiredSize + 16
success = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, DeviceInterfaceData, DeviceInterfaceDetailData, DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData)
//Success is false. GetLastError() shows 1784, and DeviceInterfaceDetailData does not get set, so I can't get DeviceInterfaceDetailData.DevicePath