hab mir nen code gesucht, aller dings geht er bei mir nicht, er ist teilweise aus anderen sprachen übersetzt, er soll überrpüfen ob ein gewissen usb-gerät angestekc ist oder nicht...
Private Function FindDevice(ByVal classGuid As Guid) As String
Dim handle As Long = SetupDiGetClassDevs(classGuid, 0, 0, DIGCF_PRESENT _
Or DIGCF_DEVICEINTERFACE)
If handle = -1 Then
Throw New Exception(String.Format("Failed in call to" & _
"SetupDiGetClassDevs ({0})", GetLastError()))
End If
Dim devicePath As String = vbNullString
For deviceIndex As Integer = 0 To 100
Dim deviceInfoData As DeviceInfoData = New DeviceInfoData()
deviceInfoData.Size = Len(deviceInfoData)
If Not SetupDiEnumDeviceInfo(handle, deviceIndex, deviceInfoData) _
Then
' out of devices or do we have an error?
If Not GetLastError() = &H103 And Not GetLastError() = &H7E Then
SetupDiDestroyDeviceInfoList(handle)
Throw New Exception(String.Format("Failed in call to" & _
"SetupDiEnumDeviceInfo ({0})", GetLastError()))
End If
SetupDiDestroyDeviceInfoList(handle)
Continue For
End If
Dim deviceInterfaceData As DeviceInterfaceData = New _
DeviceInterfaceData()
deviceInterfaceData.Size = Len(deviceInterfaceData)
If Not SetupDiEnumDeviceInterfaces(handle, deviceInfoData, _
classGuid, 0, deviceInterfaceData) Then
SetupDiDestroyDeviceInfoList(handle)
Throw New Exception(String.Format("Failed in call to" & _
"SetupDiEnumDeviceInterfaces ({0})", GetLastError()))
End If
Dim cbData As Integer = 0
If Not SetupDiGetDeviceInterfaceDetail(handle, deviceInterfaceData, _
0, 0, cbData, 0) And cbData = 0 Then
SetupDiDestroyDeviceInfoList(handle)
Throw New Exception(String.Format("Failed in call to" & _
"SetupDiGetDeviceInterfaceDetail ({0})", GetLastError()))
End If
Dim deviceInterfaceDetailData As DeviceInterfaceDetailData = New _
DeviceInterfaceDetailData()
deviceInterfaceDetailData.Size = 5
If Not SetupDiGetDeviceInterfaceDetail(handle, deviceInterfaceData, _
deviceInterfaceDetailData, cbData, 0, 0) Then
SetupDiDestroyDeviceInfoList(handle)
Throw New Exception(String.Format("Failed in call to" & _
"SetupDiGetDeviceInterfaceDetail ({0})", GetLastError()))
End If
If Not deviceInterfaceDetailData.DevicePath.IndexOf( _
"vid_045e&pid_0284") = -1 Then
SetupDiDestroyDeviceInfoList(handle)
devicePath = deviceInterfaceDetailData.DevicePath
Exit For
End If
Next deviceIndex
Return devicePath
End Function
'Aufruf nur as beispiel (is dann in ner Load event drin:
Dim classguid As System.Guid
HidD_GetHidGuid(classguid)
Me.Text = FindDevice(classguid) bei mir kommt immer ein fehler das ich nicht durch 0 dividieren darf, OverflowException oder so
vll findet wer den fehler
bei euch wird's wenn ohne fehler geht warscheinlich immer nen leeren string zurückgeben weil ihr das gerät nicht habt, aber vll könnt ihr ja selber kurz die kennung (bei mir "vid_045e&pid_0284") ändern, und testen.
mfg
Wer fr?her stirbt ist l?nger tot! |