Code Teil 2
Private Function FindDevice(ByVal classGuid As Guid) As String
'Dim handle As Int32 = SetupDiGetClassDevs(classGuid, 0, 0, DIGCF_PRESENT Or
' DIGCF_DEVICEINTERFACE)
Dim handle As Int32 = SetupDiGetClassDevs(classGuid, vbNullString, 0, _
DIGCF_ALLCLASSES)
Dim errNum% = Marshal.GetLastWin32Error
If handle = -1 Then
Throw New ApiException("Failed in call to SetupDiGetClassDevs", errNum)
End If
Try
Dim devicePath As String = vbNullString
Dim deviceIndex As Integer = 0
Do
Dim deviceInfoData As DeviceInfoData = New DeviceInfoData()
deviceInfoData.Size = Marshal.SizeOf(deviceInfoData)
If Not SetupDiEnumDeviceInfo(handle, deviceIndex, deviceInfoData) Then
errNum = Marshal.GetLastWin32Error
If errNum = ERROR_NO_MORE_ITEMS Then
Debug.WriteLine("No more items bei DeviceInfo mit DeviceIdx: " & _
deviceIndex)
Exit Do
Else
Throw New ApiException("SetupDiEnumDeviceInfo", errNum)
End If
End If
Debug.WriteLine("Gefunden DeviceInfo mit DeviceIdx: " & deviceIndex)
Dim deviceInterfaceData As DeviceInterfaceData = New DeviceInterfaceData()
deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData)
Dim idxInterface% = 0
Do
If Not SetupDiEnumDeviceInterfaces(handle, deviceInfoData, classGuid, _
idxInterface, deviceInterfaceData) Then
errNum = Marshal.GetLastWin32Error
If errNum = ERROR_NO_MORE_ITEMS Then
Debug.WriteLine(ControlChars.Tab & "No more Items bei Interface mit" & _
"Memeberindex " & idxInterface)
Exit Do
Else
Throw New ApiException("SetupDiEnumDeviceInfo", errNum)
End If
End If
Debug.WriteLine(ControlChars.Tab & "Gefunden Interface mit Memeberindex " _
& idxInterface)
idxInterface += 1
Loop
deviceIndex += 1
Loop
Return ""
Catch ex As Exception
Debug.WriteLine(ex.ToString)
Throw
Finally
SetupDiDestroyDeviceInfoList(handle)
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
Dim id As Guid = Guid.Empty
HidD_GetHidGuid(id)
Try
Debug.WriteLine(FindDevice(id))
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
End Sub
Public Class ApiException
Inherits Exception
Public Sub New(ByVal errNum%)
MyBase.New(GetApiError(errNum))
End Sub
Public Sub New(ByVal msg$, ByVal errNum%)
MyBase.New(msg & " " & GetApiError(errNum))
End Sub
Private Shared Function GetApiError(ByVal errNum%) As String
Dim sb As New System.Text.StringBuilder(512)
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, errNum, 0, sb, sb.Capacity, 0)
Return sb.ToString
End Function
End Class
End Class |