Click here to Skip to main content
15,122,379 members
Articles / Desktop Programming / MFC
Posted 20 Jul 2002


37 bookmarked

Enumerating serial ports - W2K style.

Rate me:
Please Sign up or sign in to vote.
4.56/5 (9 votes)
20 Jul 20021 min read
Enumerating the serial ports using the SetupDi* API provided with Win2K and later


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.

The attached serial port enumeration code first determines which operating system it is running under, and then runs the appropriate routine to enumerate the serial ports. In Win 9x (and Me) it uses the registry. In W2K and later it uses the SetupAPI that was included in that version of the WinSDK. It also has support for "brute force" enumeration of serial ports under NT4.

Unfortunately, I statically linked with setupapi.lib, so the provided executable won't actually run under 95 and nt4 (I didn't really need to support those OS's 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 (i.e. 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


CArray<SSerInfo,SSerInfo&> asi;
EnumSerialPorts(asi,FALSE/*include all*/);


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Zach Gorman
United States United States
No Biography provided

Comments and Discussions

QuestionEnumSerial class Pin
alkam11-Aug-11 10:52
Memberalkam11-Aug-11 10:52 
Maciej Polanczyk11-Dec-08 21:55
MemberMaciej Polanczyk11-Dec-08 21:55 
GeneralQuestion on bidirectional USB Pin
gbeez22-May-08 11:49
Membergbeez22-May-08 11:49 
QuestionHow can I get Serial port properties? Pin
Md. Mazharul Islam Khan4-Feb-08 20:08
MemberMd. Mazharul Islam Khan4-Feb-08 20:08 
QuestionLNK2019 error Pin
bob go4-Nov-07 20:46
Memberbob go4-Nov-07 20:46 
QuestionTest for connection to USB bus Pin
say.hello5-Oct-06 9:51
Membersay.hello5-Oct-06 9:51 
GeneralObtaining the PortName Pin
PatrickA26-Nov-04 2:01
MemberPatrickA26-Nov-04 2:01 
GeneralRemoval relations Pin
yonalevine9-Nov-04 23:29
Memberyonalevine9-Nov-04 23:29 
GeneralUsing SetupAPI Functions Pin
mtaghiloo8-Jun-04 0:03
Membermtaghiloo8-Jun-04 0:03 
GeneralClaim/Release SerialPort Pin
orfeas2913-Apr-04 1:08
Memberorfeas2913-Apr-04 1:08 
GeneralUSB/Serial cables in Win98 &amp; WinME Pin
Howard C. Anderson10-Nov-03 6:26
MemberHoward C. Anderson10-Nov-03 6:26 
QuestionHow to identify application which has access to port? Pin
Joerg Hoffmann11-Apr-03 0:35
MemberJoerg Hoffmann11-Apr-03 0:35 
AnswerRe: How to identify application which has access to port? Pin
Zach Gorman11-Apr-03 6:00
MemberZach Gorman11-Apr-03 6:00 
QuestionAny idea how to get friendly names on NT? Pin
Joerg Hoffmann10-Apr-03 5:54
MemberJoerg Hoffmann10-Apr-03 5:54 
AnswerRe: Any idea how to get friendly names on NT? Pin
Zach Gorman10-Apr-03 7:19
MemberZach Gorman10-Apr-03 7:19 
GeneralRe: Any idea how to get friendly names on NT? Pin
Joerg Hoffmann10-Apr-03 21:22
MemberJoerg Hoffmann10-Apr-03 21:22 
GeneralFTDI(Win98) and PCI modem(WinXP) Pin
PabloII22-Dec-02 21:59
MemberPabloII22-Dec-02 21:59 
GeneralRe: FTDI(Win98) and PCI modem(WinXP) Pin
Zach Gorman7-Jan-03 10:40
MemberZach Gorman7-Jan-03 10:40 
GeneralRe: FTDI(Win98) and PCI modem(WinXP) Pin
Christian B21-Apr-03 10:05
MemberChristian B21-Apr-03 10:05 
GeneralRe: FTDI(Win98) and PCI modem(WinXP) Pin
SamuliA5-Sep-06 0:26
MemberSamuliA5-Sep-06 0:26 
GeneralRe: FTDI(Win98) and PCI modem(WinXP) Pin
Taulie4-Nov-07 22:32
MemberTaulie4-Nov-07 22:32 
GeneralData Structure Item Missing in EnumPortsWdm Pin
avidgator7-Aug-02 6:25
Memberavidgator7-Aug-02 6:25 
GeneralRe: Data Structure Item Missing in EnumPortsWdm Pin
Zach Gorman10-Aug-02 8:36
MemberZach Gorman10-Aug-02 8:36 
GeneralRe: Data Structure Item Missing in EnumPortsWdm Pin
Christian B21-Apr-03 10:02
MemberChristian B21-Apr-03 10:02 
GeneralRe: Data Structure Item Missing in EnumPortsWdm Pin
Member 74665210-Jun-04 4:40
MemberMember 74665210-Jun-04 4:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.