According to this stack overflow thread, PJ Naughter has implemented 9 methods to emunerate com port in Windows. That code was named EnumSerialPorts.
As my using, I found some method would list non-existed com ports. for example:
My computer com port be :
But the output of PJ Naughte's EnumSerialPorts be :
Otherwise, that code are too extravagant, those is hard to separate into an individual function: it is too much dependency on the other functions.
I has reorganized those functions being independent forms in C(instead of C++), those are more portable and useful. I also eliminated some methods which would report unused com ports.
My code be :
The output be :
Note that: EnumerateComPortSetupAPI_GUID_DEVINTERFACE_COMPORT
would not list CH340 port, which is on an Arduino UNO board. (But the same function could list other CH340 port, if it is not on this Arduino). Besides, this function could not list Bluetooth virtual com ports.
If you would like to use my code, I suggest you use function
That is the best one which balances speed and detail; but this one are most complicated in implementation. Otherwise, if you want to implement a function which could list com ports as simple as possible, I recommend use the CreateFile method: that is rudimentary, but sufficient.