Click here to Skip to main content
Licence 
First Posted 20 Jul 2002
Views 142,067
Bookmarked 34 times

Enumerating serial ports - W2K style.

By | 20 Jul 2002 | Article
Enumerating the serial ports using the SetupDi* API provided with Win2K and later

Introduction

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

Example

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

License

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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionEnumSerial class Pinmemberalkam9:52 11 Aug '11  
GeneralERROR_INVALID_USER_BUFFER PinmemberMaciej Polanczyk20:55 11 Dec '08  
GeneralQuestion on bidirectional USB Pinmembergbeez10:49 22 May '08  
QuestionHow can I get Serial port properties? Pinmembermazhar_cse19:08 4 Feb '08  
QuestionLNK2019 error Pinmemberbob go19:46 4 Nov '07  
QuestionTest for connection to USB bus Pinmembersay.hello8:51 5 Oct '06  
GeneralObtaining the PortName PinmemberPatrickA1:01 26 Nov '04  
GeneralRemoval relations Pinmemberyonalevine22:29 9 Nov '04  
GeneralUsing SetupAPI Functions Pinsussmajid taghiloo23:03 7 Jun '04  
GeneralClaim/Release SerialPort Pinmemberorfeas290:08 13 Apr '04  
GeneralUSB/Serial cables in Win98 & WinME PinmemberHoward C. Anderson5:26 10 Nov '03  
QuestionHow to identify application which has access to port? PinmemberJoerg Hoffmann23:35 10 Apr '03  
AnswerRe: How to identify application which has access to port? PinmemberZach Gorman5:00 11 Apr '03  
QuestionAny idea how to get friendly names on NT? PinmemberJoerg Hoffmann4:54 10 Apr '03  
AnswerRe: Any idea how to get friendly names on NT? PinmemberZach Gorman6:19 10 Apr '03  
GeneralRe: Any idea how to get friendly names on NT? PinmemberJoerg Hoffmann20:22 10 Apr '03  
GeneralFTDI(Win98) and PCI modem(WinXP) PinsussPabloII20:59 22 Dec '02  
GeneralRe: FTDI(Win98) and PCI modem(WinXP) PinmemberZach Gorman9:40 7 Jan '03  
GeneralRe: FTDI(Win98) and PCI modem(WinXP) PinmemberChristian B9:05 21 Apr '03  
GeneralRe: FTDI(Win98) and PCI modem(WinXP) PinmemberSamuliA23:26 4 Sep '06  
GeneralRe: FTDI(Win98) and PCI modem(WinXP) Pinmembergrandmasta121:32 4 Nov '07  
GeneralData Structure Item Missing in EnumPortsWdm Pinmemberavidgator5:25 7 Aug '02  
GeneralRe: Data Structure Item Missing in EnumPortsWdm PinmemberZach Gorman7:36 10 Aug '02  
GeneralRe: Data Structure Item Missing in EnumPortsWdm PinmemberChristian B9:02 21 Apr '03  
GeneralRe: Data Structure Item Missing in EnumPortsWdm PinmemberPeter Wurmsdobler3:40 10 Jun '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 21 Jul 2002
Article Copyright 2002 by Zach Gorman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid