How to detect serial ports

Posted on 1998-10-21
Last Modified: 2013-12-03
I want to populate a combo box with a list of serial ports actually available on a system.  Any ideas?  I thought about just attempting to open COM1-COM8 and looking at return code, but I also want to show any ports that have a friendly name.
Question by:rosborn051498
  • 3
  • 3
  • 3

Expert Comment

ID: 1415507
The Win32 API does not provide any mechanism for determining what ports exist on a system. Windows NT and Windows 95 keep track of installed ports differently from one another, so any one method would not be portable across all Win32 platforms. Some systems even have more ports than the traditional maximum of four. Hardware vendors and serial-device-driver writers are free to name the ports anything they like. For this reason, it is best that users have the ability to specify the port name they want to use.

The CreateFile function opens a communications port. If a port does not exist, an error will occur (ERROR_FILE_NOT_FOUND) after attempting to open the port, and the user should be notified that the port isn’t available.

Author Comment

ID: 1415508
I knew all that already.  However, I have seen apps that can somehow "intelligently" display only the ports that are actually installed.  And this app works under both 95 and NT.  Perhaps they do the CreateFile and look for ERROR_FILE_NOT_FOUND, but I'm hoping to find out if there's a better/cleaner way to do it.  Maybe an undocumented VCOMM call?

I can also walk the registry and look in LOCAL_MACHINE\enum\Root\Ports, but some may not show up.

Expert Comment

ID: 1415509
To use the VCOMM (only Win95) services directly you have to write a VxD.
The best way is to walk the port numbers and call the CreateFile function.
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Author Comment

ID: 1415510
What a shame.  Remember the good 'ol days when you could just read the BIOS data area (40:0)?  Oh well, thanks for the answer.  I'll give you the points just for responding so quickly.
LVL 15

Accepted Solution

NickRepin earned 100 total points
ID: 1415511
Look for them under registry key LOCAL_MACHINE/devicemap/COM1...
LVL 15

Expert Comment

ID: 1415512
More precisely,
2)Enum all values: Serial0="COM1"

Author Comment

ID: 1415513
Let me provide a litle more background.  I have a static port driver that emulates COM ports.  Currently, it does not create devicemap\serialcomm entries for the COM ports that it creates (it calls VCOMM_Add_Port).  Now, only the physical ports show up in devicemap\serialcomm.

Should the driver add a serialcomm entry for each port it creates?

Expert Comment

ID: 1415514
VCOMM is called to load the COM/LPT port drivers when they are enumerated by the Plug and Play Configuration Manager. This is at boot time for COM/LPT ports.
When the Plug and Play Configuration Manager(CM) calls VCOMM to load an enumerated device, it passes a handle to the "devnode" data structure for the device. The devnode contains information about the device, including its resource allocation and REGISTRY key locations.
Your driver should notify the CM (create the devnode) about the new ports. Use the CONFIGMG_XXX_XXX services such as CONFIGMG_Create_DevNode, CONFIGMG_Get_Child, CONFIGMG_Locate_DevNode, CONFIGMG_Setup_DevNode, etc
LVL 15

Expert Comment

ID: 1415515
rosborn, that is an another question.
I've never added serial ports from my device driver before.
Also, I'm an expert more in NT than in 95.

I can only give you advice to look for information you need in DDK docs or Also, you can try to manually add registry enties and see what happen.

The only thing I'm sure in is that if you want to obtain a full list of serial ports, you should <see my answer>.

Here is some information from Windows NT Resource Kit (regentry.hlp). Of course, it contains a bit more useful information. Hope it helps.

Deleting Serial Ports

If you need to delete one or more COM ports, you can do this only by deleting the related Registry information in the DeviceMap and Services areas of the Registry. To delete entries in the DeviceMap subkey, you must be logged on as a member of the Administrators group.
To delete a COM port

1.      In Registry Editor, delete the appropriate value entry for the COM port under the following Registry path:


The value entries in this subkey are used to determine all the communication ports available on the system. The values are of the form Serialyyy=COMx, where COMx can be any value from COM1 through COM256, and Serialyyy can be a value from Serial0 through any large positive number, such as Serial15000.
You can identify the value entry you want to delete based on the list you can see by using Ports in Control Panel.

2.      Delete the COM port's related Serialyyy key in the following Registry path:


If you do not delete the related Services subkey for Serialyyy, the COMx port continues to appear in the Ports list in Control Panel each time the system starts, even though there is no related DeviceMap subkey.

DeviceMap Subkey Entries for Serial and Parallel Ports

Remember that the entries in HKEY_LOCAL_MACHINE\Hardware are recreated each time the system is started. The entries in these subkeys are described here for information only.

ParallelX     REG_SZ    

Path:           HKEY_LOCAL_MACHINE\Hardware\Devicemap\Parallel ports

Range:  A string, typically LPTy

Specifies that the Windows NT Parallel device X is the actual device for the MS-DOS name LPTy. These value entries are used to determine all the parallel ports available on the system.

SerialX     REG_SZ    

Path            HKEY_LOCAL_MACHINE\Hardware\Devicemap\Serialcomm
Range: A string, typically COMy

Specifies that the Windows NT Serial device X is the actual device for the MS-DOS name COMy. These value entries are used to determine all the communication ports available on the system.


Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question