Click here to Skip to main content
11,704,107 members (65,875 online)
Click here to Skip to main content

Enumerating serial ports - W2K style.

, 20 Jul 2002 163.1K 6.4K 36
Rate this:
Please Sign up or sign in to vote.
Enumerating the serial ports using the SetupDi* API provided with Win2K and later
<!-- Download Links --> <!-- Add the rest of your HTML here -->

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

Share

About the Author

Zach Gorman
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionEnumSerial class Pin
alkam11-Aug-11 9:52
memberalkam11-Aug-11 9:52 
GeneralERROR_INVALID_USER_BUFFER Pin
Maciej Polanczyk11-Dec-08 20:55
memberMaciej Polanczyk11-Dec-08 20:55 
GeneralQuestion on bidirectional USB Pin
gbeez22-May-08 10:49
membergbeez22-May-08 10:49 
QuestionHow can I get Serial port properties? Pin
mazhar_cse4-Feb-08 19:08
membermazhar_cse4-Feb-08 19:08 
QuestionLNK2019 error Pin
bob go4-Nov-07 19:46
memberbob go4-Nov-07 19:46 
QuestionTest for connection to USB bus Pin
say.hello5-Oct-06 8:51
membersay.hello5-Oct-06 8:51 
GeneralObtaining the PortName Pin
PatrickA26-Nov-04 1:01
memberPatrickA26-Nov-04 1:01 
GeneralRemoval relations Pin
yonalevine9-Nov-04 22:29
memberyonalevine9-Nov-04 22:29 
GeneralUsing SetupAPI Functions Pin
majid taghiloo7-Jun-04 23:03
sussmajid taghiloo7-Jun-04 23:03 
GeneralClaim/Release SerialPort Pin
orfeas2913-Apr-04 0:08
memberorfeas2913-Apr-04 0:08 
GeneralUSB/Serial cables in Win98 &amp; WinME Pin
Howard C. Anderson10-Nov-03 5:26
memberHoward C. Anderson10-Nov-03 5:26 
QuestionHow to identify application which has access to port? Pin
Joerg Hoffmann10-Apr-03 23:35
memberJoerg Hoffmann10-Apr-03 23:35 
AnswerRe: How to identify application which has access to port? Pin
Zach Gorman11-Apr-03 5:00
memberZach Gorman11-Apr-03 5:00 
QuestionAny idea how to get friendly names on NT? Pin
Joerg Hoffmann10-Apr-03 4:54
memberJoerg Hoffmann10-Apr-03 4:54 
AnswerRe: Any idea how to get friendly names on NT? Pin
Zach Gorman10-Apr-03 6:19
memberZach Gorman10-Apr-03 6:19 
GeneralRe: Any idea how to get friendly names on NT? Pin
Joerg Hoffmann10-Apr-03 20:22
memberJoerg Hoffmann10-Apr-03 20:22 
GeneralFTDI(Win98) and PCI modem(WinXP) Pin
PabloII22-Dec-02 20:59
sussPabloII22-Dec-02 20:59 
GeneralRe: FTDI(Win98) and PCI modem(WinXP) Pin
Zach Gorman7-Jan-03 9:40
memberZach Gorman7-Jan-03 9:40 
GeneralRe: FTDI(Win98) and PCI modem(WinXP) Pin
Christian B21-Apr-03 9:05
memberChristian B21-Apr-03 9:05 
GeneralRe: FTDI(Win98) and PCI modem(WinXP) Pin
SamuliA4-Sep-06 23:26
memberSamuliA4-Sep-06 23:26 
GeneralRe: FTDI(Win98) and PCI modem(WinXP) Pin
grandmasta14-Nov-07 21:32
membergrandmasta14-Nov-07 21:32 
GeneralData Structure Item Missing in EnumPortsWdm Pin
avidgator7-Aug-02 5:25
memberavidgator7-Aug-02 5:25 
GeneralRe: Data Structure Item Missing in EnumPortsWdm Pin
Zach Gorman10-Aug-02 7:36
memberZach Gorman10-Aug-02 7:36 
GeneralRe: Data Structure Item Missing in EnumPortsWdm Pin
Christian B21-Apr-03 9:02
memberChristian B21-Apr-03 9:02 
Hi,

Attached is my version of portlister for w2k that I used a couple of years ago. There is a SetupApi for W9x aswell that can be used with the same result, not suplied here.

Pros:
It uses the ClassName "PORTS" instead of the hard-coded guid.
It also extracts the PortName from the registry in a proper way.

Enjoy!
// Christian


// portlister.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "tchar.h"
#include "setupapi.h"

int main(int argc, char* argv[])
{
GUID ClassGuid[1];
DWORD dwRequiredSize;
BOOL bRet;
HDEVINFO DeviceInfoSet = NULL;

SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

DWORD dwMemberIndex = 0;

// Get ClassGuid from ClassName for PORTS class
bRet = SetupDiClassGuidsFromName(_T("PORTS"), (LPGUID)&ClassGuid, 1, &dwRequiredSize);
if (!bRet) goto cleanup;

// Get class devices
DeviceInfoSet = SetupDiGetClassDevs(ClassGuid, NULL, NULL, DIGCF_PROFILE);

if (DeviceInfoSet)
{
// Enumerate devices
dwMemberIndex = 0;
while (SetupDiEnumDeviceInfo(DeviceInfoSet, dwMemberIndex++, &DeviceInfoData))
{
TCHAR szFriendlyName[MAX_PATH];
TCHAR szPortName[MAX_PATH];
TCHAR szMessage[MAX_PATH];
DWORD dwReqSize = 0;
DWORD dwPropType;
DWORD dwType = REG_SZ;
HKEY hKey = NULL;

// Get friendlyname
bRet = SetupDiGetDeviceRegistryProperty(DeviceInfoSet,
&DeviceInfoData,
SPDRP_FRIENDLYNAME,
&dwPropType,
(LPBYTE)szFriendlyName,
sizeof(szFriendlyName),
&dwReqSize);

// Open device parameters reg key
hKey = SetupDiOpenDevRegKey(DeviceInfoSet,
&DeviceInfoData,
DICS_FLAG_GLOBAL,
0,
DIREG_DEV,
KEY_READ);
if (hKey)
{
// Qurey for portname
dwReqSize = sizeof(szPortName);
long lRet = RegQueryValueEx(hKey,
_T("PortName"),
0,
&dwType,
(LPBYTE)&szPortName,
&dwReqSize);

// Close reg key
RegCloseKey(hKey);
}

wsprintf(szMessage, _T("Name: %s\nPort: %s\n"), szFriendlyName, szPortName);
MessageBox(NULL, szMessage, _T("Found Port"), MB_OK);
}
}

cleanup:
// Destroy device info list
SetupDiDestroyDeviceInfoList(DeviceInfoSet);

return 0;
}

GeneralRe: Data Structure Item Missing in EnumPortsWdm Pin
Peter Wurmsdobler10-Jun-04 3:40
memberPeter Wurmsdobler10-Jun-04 3:40 
GeneralToo complicated -> use GetDefaultCommConfig() Pin
Andre Gleichner22-Jul-02 2:17
memberAndre Gleichner22-Jul-02 2:17 
GeneralRe: Too complicated -> use GetDefaultCommConfig() Pin
Zach Gorman22-Jul-02 18:27
memberZach Gorman22-Jul-02 18:27 
GeneralRe: Too complicated -> use GetDefaultCommConfig() Pin
Joerg Hoffmann24-Apr-03 21:46
memberJoerg Hoffmann24-Apr-03 21:46 
GeneralRe: Too complicated -&gt; use GetDefaultCommConfig() Pin
Mike Pulice23-Dec-03 6:26
memberMike Pulice23-Dec-03 6:26 
GeneralRe: Too complicated -&gt; use GetDefaultCommConfig() Pin
Zach Gorman4-Jan-04 14:23
memberZach Gorman4-Jan-04 14:23 
GeneralRe: Too complicated -&gt; use GetDefaultCommConfig() [modified] Pin
CrashTest31-Aug-09 9:22
memberCrashTest31-Aug-09 9:22 

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.

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