ListPorts is a function that lists all serial ports available on the system, along with some descriptive text suitable to be shown to the user in place of the somewhat terse "COM1", "COM2", etc. You can see an example of this on the "Port" combobox at the system Modem properties dialog box.
PJ Naughter has already wrote some code with this very purpose (see
EnumSerialPorts at CodeProject, or click here for the latest version of his library).
ListPorts has, IMHO, some advantages with respect to PJ Naughter's approach:
- it does not depend on MFC,
- it seems to handle non-standard ports (virtual, infrared) somewhat better,
- descriptive text is provided for each port,
- works for Windows CE.
Anyway, we wouldn't like to be blamed for proselytism: please compare both approaches and make your own choice.
listports.h header provides the following C language definitions:
typedef BOOL (CALLBACK* LISTPORTS_CALLBACK)(LPVOID lpCallbackValue,
BOOL ListPorts(LISTPORTS_CALLBACK lpCallback,LPVOID lpCallbackValue);
LISTPORTS_PORTINFO holds information about a particular serial port:
lpPortName holds the typical "COMn" string with which one can get a handle to the port via
lpFriendlyName holds a fuller description of the port (vg., "Infrared Communications Port (COM4)"). On those systems where it is supported,
lpTechnology names the technology upon which the serial port operates: typical values are "BIOS", "INFRARED", "USB", etc.
LISTPORTS_CALLBACK defines a user-supplied callback routine that is provided the information on each serial port available on the system on successive calls from
ListPorts. Do in your callback whatever that suits your needs: dump the information on to the console, plug it into a listbox or store it for later use. Please note, however, that the strings stored on the
LISTPORTS_PORTINFO are not to be referenced after the callbacks returns: so, if you plan on storing the values you should make private copies of them instead.
If your callback returns
FALSE, the enumeration is aborted.
ListPorts accepts an additional parameter named
lpCallbackValue. This is treated opaquely by the library and passed to your callback, so that you can use it for your particular purposes (telling between different invocations to
ListPorts or storing a pointer to some object responsible of using the results, for instance). This is a standard technique used in many callback-based APIs, anyway.
Please see the demo project for a particularly simple example of the use of the library.
ListPorts works on the following OSs:
- Windows 95, 98, ME
- Windows NT 4.0
- Windows 2000, XP
- Windows CE
But there are several shortcomings depending on the operating system. On NT 4.0, we haven't been able to locate the description text for the serial ports (actually we suspect NT 4.0 does not have these): instead, the bare "COMn" strings are supplied. The
lpTecnhlogy field is not available for NT 4.0 and Windows CE platforms.
Windows CE devices have vendor-specific customizations of the operating system low-level layers which may not adjust to the algorithm used by
ListPorts: our practical tests have shown, however, that the probability of
ListPorts missing to locate some ports in Windows CE is very low.
The code compiles and works just fine in Unicode, with macros
If you don't have curiosity about the internals of the library, skip this section.
ListPorts finds the information on the serial ports available on the system by scanning the registry. On Windows 9x platforms, information on installed devices is stored under the
HKEY_LOCAL_MACHINE\Enum key. The enumeration tree has three levels, and devices are described at the deepest one. For instance, a standard UART serial port built into the motherboard and recognized by the BIOS could be stored like this:
|-0D (or any other value, this is not important for us)
· CLASS= "Ports"
· PORTNAME= "COM1"
· FRIENDLYNAME= "Communications Port (COM1)"
CLASS identifies the type of device and is used by
ListPorts to pinpoint the communications ports.
On Windows 2000/XP, the situation is very similar, except that the enumeration tree is located at
CLASS is deprecated in favor of a
CLASSGUID identifier based on COM unique identifiers.
Windows NT 4.0 lacks a fully developed device enumeration tree. Information on serial ports can be found at
HKEY_LOCAL_MACHINE\Hardware\DEVICEMAP\SERIALCOMM, but no
FRIENDLYNAME is provided here.
Windows CE stores serial port entries along with other communications ports under
The method used by the library is discussed in greater detail at the comments of the code in listports.c file.
New in version 2.0 (August 2005)
- Support for Windows CE platforms.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.