|
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 written 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:
Anyway, I 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 struct { LPCTSTR lpPortName; LPCTSTR lpFriendlyName; }LISTPORTS_PORTINFO; typedef BOOL (CALLBACK* LISTPORTS_CALLBACK)(LPVOID lpCallbackValue, LISTPORTS_PORTINFO* lpPortInfo); 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 CreateFile()
, whereas lpFriendlyName
holds a fuller description of the port (vg., "Infrared Communications Port
(COM4)").
LISTPORTS_CALLBACK
defines a user-supplied callback routine that
is provided the information on each serial port available on the system on
succesive 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 use of the library.
ListPorts
works on the following OSs:
The functionality is less rich on Windows NT 4.0, however. On this plaftorm, I haven't been able to locate the description text for the serial ports (actually I suspect NT 4.0 does not have these). Instead, the bare "COMn" strings are supplied.
I don't know if this library works well on Windows XP. If someone out there makes the test, please let me know.
The code compiles and works just fine in Unicode, with macros UNICODE
and _UNICODE
defined.
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:
HKLM\ENUM |-BIOS |-*PNP0501 |-0D (or any other value, this is not important for us) · CLASS= "Ports" · PORTNAME= "COM1" · FRIENDLYNAME= "Communications Port (COM1)"
The value CLASS
identifies the type of device and is used by ListPorts
to pinpoint the communications ports.
On Windows 2000, the situation is very similar, except that the enumeration
tree is located at HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum
,
and 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.
The method used by the library is discussed in greater detail at the comments
of the code in listports.c
file.
Premium Sponsor |
|
Home >>
System >>
General
Updated: 17 Jul 2001 Editor: Chris Maunder |
Article content copyright Joaquín M López Muñoz, 2001 everything else © CodeProject, 1999-2002. Advertise on The Code Project |
DevelopersDex • DevGuru • Programmers Heaven • Tek-Tips Forums • TopXML • VisualBuilder.com • W3Schools • XMLPitstop • ZVON • Search all Partners |