Я могу получить и перечислить все имена виртуальных коммуникационных портов USB-последовательный порт в моем приложении, но не могу получить описание устройства, о котором сообщается по шине, для любого данного порта / устройства, которое я хотел бы сравнить с конкретная строка для сопоставления с конкретным конвертером ... существуют ли какие-либо интерфейсы vb.net или вызовы правильной настройки setupapi.dll, которые возвращают описание устройства, сообщенное шиной?
Примечание. Использование VS2017 на компьютере под управлением Windows 10.
Текущий код для перечисления портов выглядит следующим образом:
<DllImport("setupapi.dll")>
Private Shared Function SetupDiClassGuidsFromNameA(ByVal ClassN As String, ByRef guids As Guid, ByVal ClassNameSize As UInt32, ByRef ReqSize As UInt32) As Boolean
End Function
<DllImport("setupapi.dll")>
Private Shared Function SetupDiGetClassDevsA(ByRef ClassGuid As Guid, ByVal Enumerator As UInt32, ByVal hwndParent As IntPtr, ByVal Flags As UInt32) As IntPtr
End Function
<DllImport("setupapi.dll")>
Private Shared Function SetupDiEnumDeviceInfo(ByVal DeviceInfoSet As IntPtr, ByVal MemberIndex As UInt32, ByVal DeviceInfoData As SP_DEVINFO_DATA) As Boolean
End Function
<DllImport("setupapi.dll")>
Private Shared Function SetupDiDestroyDeviceInfoList(ByVal DeviceInfoSet As IntPtr) As Boolean
End Function
<DllImport("setupapi.dll")>
Private Shared Function SetupDiGetDeviceRegistryPropertyA(ByVal DeviceInfoSet As IntPtr, ByVal DeviceInfoData As SP_DEVINFO_DATA, ByVal Propert As UInt32, ByVal PropertyRegDataType As UInt32, ByVal PropertyBuffer As StringBuilder, ByVal PropertyBufferSize As UInt32, ByVal RequiredSize As IntPtr) As Boolean
End Function
Private Shared Function EnumerateDevices(ByVal DeviceIndex As UInt32, ByVal ClassName As String, ByVal DeviceName As StringBuilder) As Integer
Dim RequiredSize As UInt32 = 0
Dim guid As Guid = Guid.Empty
Dim guids As Guid() = New Guid(0) {}
Dim NewDeviceInfoSet As IntPtr
Dim DeviceInfoData As SP_DEVINFO_DATA = New SP_DEVINFO_DATA()
Dim res As Boolean = SetupDiClassGuidsFromNameA(ClassName, guids(0), RequiredSize, RequiredSize)
If RequiredSize = 0 Then
'incorrect class name:
DeviceName = New StringBuilder("")
Return -2
End If
If (Not res) Then
guids = New Guid(System.Convert.ToInt32(RequiredSize) - 1) {}
res = SetupDiClassGuidsFromNameA(ClassName, guids(0), RequiredSize, RequiredSize)
If (Not res) OrElse RequiredSize = 0 Then
'incorrect class name:
DeviceName = New StringBuilder("")
Return -2
End If
End If
'get device info set for our device class
NewDeviceInfoSet = SetupDiGetClassDevsA(guids(0), 0, IntPtr.Zero, DIGCF_PRESENT)
If NewDeviceInfoSet.ToInt32() = -1 Then
'device information is unavailable:
DeviceName = New StringBuilder("")
Return -3
End If
DeviceInfoData.cbSize = 28
'is devices exist for class
DeviceInfoData.DevInst = 0
DeviceInfoData.ClassGuid = System.Guid.Empty
DeviceInfoData.Reserved = 0
res = SetupDiEnumDeviceInfo(NewDeviceInfoSet, DeviceIndex, DeviceInfoData)
If (Not res) Then
'no such device:
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet)
DeviceName = New StringBuilder("")
Return -1
End If
DeviceName.Capacity = MAX_DEV_LEN
If (Not SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet, DeviceInfoData, SPDRP_FRIENDLYNAME, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero)) Then
res = SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet, DeviceInfoData, SPDRP_DEVICEDESC, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero)
If (Not res) Then
'incorrect device name:
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet)
DeviceName = New StringBuilder("")
Return -4
End If
End If
Return 0
End Function
````