friendly name = the name that appears in \"Device Manager\" under \"Ports (COM & LPT).
EDIT: two solutions provided below. One with WMI and another with SetupAPI
I use virtual ports generated by com0com.
With that you're able to name the ports as you want - so I've port captions like follows:
"com0com - bus for serial port pair emulator 0 (COMA <-> COMB)"
With the above posted WMI code you will then get the name "COMA <-> COMB"
for that port.
It seems such constructs are no ports, but the above code will treat it as serial port...
BTW, "COMA"
is a totally valid name for a port... (that means, looking for only numbers at the end is not enough).
So I wonder how I can reliably distinguish between a valid, existing serial port name and such strange constructs...
I know this was posted in C#, but I am certain this can easily be converted...
Public Function foo() As Integer
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_SerialPort")
For Each queryObj As ManagementObject In searcher.Get()
Debug.WriteLine(queryObj("Caption"))
Debug.WriteLine(queryObj("Description"))
Debug.WriteLine(queryObj("DeviceID"))
Debug.WriteLine(queryObj("Name"))
Debug.WriteLine(queryObj("PNPDeviceID"))
Next
Catch err As ManagementException
Stop
End Try
End Function
Public Function bar() As Integer
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0")
For Each queryObj As ManagementObject In searcher.Get()
If queryObj("Caption").ToString.Contains("(COM") Then
Debug.WriteLine(queryObj("Caption"))
Debug.WriteLine(queryObj("Description"))
Debug.WriteLine(queryObj("DeviceID"))
Debug.WriteLine(queryObj("Name"))
Debug.WriteLine(queryObj("PNPDeviceID"))
End If
Next
Catch err As ManagementException
Stop
End Try
End Function
It finds all of my com ports, modem, serial, usb, and bluetooth.