Virtual Developer Workshop: Containerized Development with Docker
You might think that determining which serial ports are present on a Windows PC would be an easy task. It seems like a reasonable enough thing for the OS to support. Unfortunately, there was no support for it at all (short of reading the Registry yourself) before Win2k, and even then, the API is a bit cumbersome.
Unfortunately, I statically linked with setupapi.lib, so the provided executable won't actually run under Windows 95 and NT4. (I didn't really need to support those OSs for my application.) This could be finagled by replacing the SetupDi* function calls with dynamic binding via LoadLibrary() if needed.
To use the EnumSerial code, simply include EnumSerial.cpp and .h in your project, and link with setupapi.lib in the WIN32 SDK (this is under "additional dependencies" in the project link settings in Visual Studio).
All you have to do now is #include "EnumSerial.h" in your source code, allocate an empty CArray of SSerInfo structs, and make a call to EnumSerialPorts. It will populate your array with filled-out SSerInfo structs which contain the following information:
CString strDevPath; // Device path for use with CreateFile() CString strPortName; // Simple name (for example, COM1) CString strFriendlyName; // Full name to be displayed to a user BOOL bUsbDevice; // Provided through a USB connection? CString strPortDesc; // Friendly name without the COMx
asi; EnumSerialPorts(asi,FALSE/*include all*/);