Okay,
ich habe das ganze umgeschieben, da ich dieses ständige SetupDiDestroyDeviceInfoList(handle) blöd fand.
Außerdem habe ich die Abfragen
If Not GetLastError() = &H103 And Not GetLastError() = &H7E
gekürzt auf
If errNum = ERROR_NO_MORE_ITEMS
da von einem anderen Fehler im SDK nicht die Rede ist.
Außerdem geht der Code nur bis SetupDiEnumDeviceInterfaces
aber bis dahin funktioniert er. Wenigstens in dem Sinn,
das keine Execptions kommen und als einziger Api-Error
der erwartete ERROR_NO_MORE_ITEMS.
Ferner habe ich
DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE
in
DIGCF_ALLCLASSES
abgeändert, da ja bei mir kein Device präsent ist.
Code Teil 1
Imports System.Runtime.InteropServices
Public Class Form1
Private Declare Function GetLastError Lib "kernel32.dll" () As Int32
Private Declare Function SetupDiEnumDeviceInfo Lib "setupapi.dll" (ByVal _
DeviceInfoSet As Int32, ByVal MemberIndex As Int32, _
ByRef DeviceInfoData As DeviceInfoData) As Boolean
Private Declare Function SetupDiDestroyDeviceInfoList Lib "setupapi.dll" ( _
ByRef DeviceInfoSet As Int32) As Int32
Private Declare Function SetupDiGetClassDevs Lib "setupapi.dll" Alias _
"SetupDiGetClassDevsA" (ByRef ClassGuid As Guid, ByVal _
Enumerator As String, ByVal hwndParent As Int32, ByVal Flags As Int32) As _
Int32
Private Declare Function SetupDiEnumDeviceInterfaces Lib "setupapi.dll" ( _
ByVal DeviceInfoSet As Int32, _
ByRef DeviceInfoData As DeviceInfoData, _
ByRef ByRefterfaceClassGuid As Guid, _
ByVal MemberIndex As Integer, _
ByRef DeviceInterfaceData As DeviceInterfaceData) As Boolean
Private Declare Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" _
Alias "SetupDiGetDeviceInterfaceDetailA" (ByRef _
DeviceInfoSet As Int32, ByRef DeviceInterfaceData As DeviceInterfaceData, _
ByRef DeviceInterfaceDetailData As DeviceInterfaceDetailData, ByVal unused1 As _
Integer, _
ByRef RequiredSize As Int32, ByRef unused3 As Integer) As Boolean
Private Declare Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" _
Alias "SetupDiGetDeviceInterfaceDetailA" (ByRef _
DeviceInfoSet As Int32, ByRef DeviceInterfaceData As DeviceInterfaceData, _
ByRef DeviceInterfaceDetailData As Integer, ByVal unused1 As Integer, _
ByRef RequiredSize As Int32, ByRef unused3 As Integer) As Boolean
Private Declare Sub HidD_GetHidGuid Lib "hid.dll" Alias "HidD_GetHidGuid" ( _
ByRef hid_guid As System.Guid)
Private Const DIGCF_ALLCLASSES As Int32 = &H4
Private Const DIGCF_DEVICEINTERFACE As Int32 = &H10
Private Const DIGCF_PRESENT As Int32 = &H2
Private Const DIGCF_PROFILE As Int32 = &H8
<Runtime.InteropServices.StructLayout( _
Runtime.InteropServices.LayoutKind.Sequential)> Private Structure _
DeviceInterfaceDetailData
Public Size As Integer
'[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
Public DevicePath As String
End Structure
<Runtime.InteropServices.StructLayout( _
Runtime.InteropServices.LayoutKind.Sequential)> Private Structure _
DeviceInfoData
Public Size As Integer
Public Classs As Guid
Public DevInstas As Integer
Public Reserved As Integer
End Structure
<Runtime.InteropServices.StructLayout( _
Runtime.InteropServices.LayoutKind.Sequential)> Private Structure _
DeviceInterfaceData
Public Size As Integer
Public Classs As Guid
Public Flags As Integer
Public Reserved As Integer
End Structure
Const ERROR_NO_MORE_ITEMS As Int32 = &H103
Private Const FORMAT_MESSAGE_FROM_SYSTEM As Int32 = &H1000
Private Declare Function FormatMessage Lib "kernel32.dll" Alias _
"FormatMessageA" ( _
ByVal dwFlags As Int32, _
ByVal lpSource As Int32, _
ByVal dwMessageId As Int32, _
ByVal dwLanguageId As Int32, _
ByVal lpBuffer As System.Text.StringBuilder, _
ByVal nSize As Int32, _
ByRef Arguments As Int32) As Int32 |