How do I get the friendly name of a COM port in Windows? How do I get the friendly name of a COM port in Windows? windows windows

How do I get the friendly name of a COM port in Windows?


A long time ago I wrote a utility for a client to do just this, but for a GPS rather than a modem.

I have just looked at it, and bits that jump-out as being possibly helpful are:

    GUID guid = GUID_DEVCLASS_PORTS;SP_DEVICE_INTERFACE_DATA interfaceData;ZeroMemory(&interfaceData, sizeof(interfaceData));interfaceData.cbSize = sizeof(interfaceData);SP_DEVINFO_DATA devInfoData;ZeroMemory(&devInfoData, sizeof(devInfoData));devInfoData.cbSize = sizeof(devInfoData);if(SetupDiEnumDeviceInfo(    hDeviceInfo,            // Our device tree    nDevice,            // The member to look for    &devInfoData    )){    DWORD regDataType;    BYTE hardwareId[300];    if(SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_HARDWAREID, &regDataType, hardwareId, sizeof(hardwareId), NULL))    {...

(You call this bit in a loop with incrementing nDevice)

and then

BYTE friendlyName[300];        if(SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_FRIENDLYNAME, NULL, friendlyName, sizeof(friendlyName), NULL))        {            strFriendlyNames += (LPCTSTR)friendlyName;            strFriendlyNames += '\n';        }

which finds the name of the device.

Hopefully that will help you in the right direction.


After you determine a Serial Port device is the one you want (by looking at its Friendly Name, by checking its parent device etc.), the proper way to get the port's name would probably be:

  • invoke SetupDiOpenDevRegKey(hDevInfo, devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ) to get the HKEY to the so-called device key
  • query this registry key for the REG_SZ value "PortName"
  • don't forget to close the HKEY :)

However, this might require so much interop in C# it's not even funny, so I don't blame you if you keep to the string parsing solution.


The information posted by Will Dean was most helpful. This is the code that eventually worked for me. Everything in the PInvoke class was taken verbatim from http://www.pinvoke.net . I did have to change a data type here or there to make it work (like when using an enum instead of a uint) but it should be easy to figure out.

internal static string GetComPortByDescription(string Description){    string Result = string.Empty;    Guid guid = PInvoke.GUID_DEVCLASS_PORTS;    uint nDevice = 0;    uint nBytes = 300;    byte[] retval = new byte[nBytes];    uint RequiredSize = 0;    uint PropertyRegDataType = 0;    PInvoke.SP_DEVINFO_DATA devInfoData = new PInvoke.SP_DEVINFO_DATA();    devInfoData.cbSize = Marshal.SizeOf(typeof(PInvoke.SP_DEVINFO_DATA));    IntPtr hDeviceInfo = PInvoke.SetupDiGetClassDevs(        ref guid,         null,         IntPtr.Zero,         PInvoke.DIGCF.DIGCF_PRESENT);    while (PInvoke.SetupDiEnumDeviceInfo(hDeviceInfo, nDevice++, ref devInfoData))    {        if (PInvoke.SetupDiGetDeviceRegistryProperty(                hDeviceInfo,                 ref devInfoData,                 PInvoke.SPDRP.SPDRP_FRIENDLYNAME,                out PropertyRegDataType,                 retval,                 nBytes,                 out RequiredSize))        {            if (System.Text.Encoding.Unicode.GetString(retval).Substring(0, Description.Length).ToLower() ==                Description.ToLower())            {                string tmpstring = System.Text.Encoding.Unicode.GetString(retval);                Result = tmpstring.Substring(tmpstring.IndexOf("COM"),tmpstring.IndexOf(')') - tmpstring.IndexOf("COM"));            } // if retval == description        } // if (PInvoke.SetupDiGetDeviceRegistryProperty( ... SPDRP_FRIENDLYNAME ...    } // while (PInvoke.SetupDiEnumDeviceInfo(hDeviceInfo, nDevice++, ref devInfoData))    PInvoke.SetupDiDestroyDeviceInfoList(hDeviceInfo);    return Result;}

I think the line Result = tmpstring.Substring(tmpstring.IndexOf("COM"),tmpstring.IndexOf(')') - tmpstring.IndexOf("COM")); is a little clumsy, suggestions on how to clean it up would be appreciated.

Thanks for your help with this matter Will, without you, I'd still be searching google.