Get com port

How to get com port properties in windows using the Win32 API

In the previous article, I have described how we can find the Com Port Id of a USB Serial device (Arduino) with the help of VID and PID. If you have not seen this article you can read it, how to get Com Port Id of a USB Serial device.

In this article, I will describe how we can get com port properties using the win32 API. In windows, all Com Port properties (Baud rate, stop bit etc) is stored in the window registry. So to get the com properties you just need to read the windows registry.

I have already written an article, how we can read and write the Windows registry using the win32 API. If you are new and don’t know how to access the window registry using the win32 API. You can see this article, Access windows registry using the win32 API.

If you want to learn more about the Windows Internals or another technology, here 10 Free days trial for you.

So let us come on the topic and see the example code. In this example, I am reading the com port setting (baud rate, stop bit ..etc) using the win32 API.

For better indentation double click on the code,

#include "stdafx.h"
#include <initguid.h>
#include <windows.h>
#include <Setupapi.h>


#define TOTAL_BYTES_READ    48
#define OFFSET_BYTES 48


BOOL ReadComConfiguration(HKEY hKeyParent, PWCHAR subkey, PWCHAR valueName, PWCHAR* readData)
{
    HKEY hKey;
    DWORD len = TOTAL_BYTES_READ;
    DWORD readDataLen = len;
    PWCHAR readBuffer = (PWCHAR)malloc(sizeof(PWCHAR) * len);
    if (readBuffer == NULL)
    {
        return FALSE;
    }
    // Check if the registry exists
    DWORD Ret = RegOpenKeyEx(hKeyParent, subkey, 0, KEY_READ, &hKey);
    if (Ret == ERROR_SUCCESS)
    {
        Ret = RegQueryValueEx(hKey, valueName, NULL, NULL, (BYTE*)readBuffer, &readDataLen);
        while (Ret == ERROR_MORE_DATA)
        {
            // Get a buffer that is big enough.
            len += OFFSET_BYTES;
            readBuffer = (PWCHAR)realloc(readBuffer, len);
            readDataLen = len;
            Ret = RegQueryValueEx(hKey, valueName, NULL, NULL, (BYTE*)readBuffer, &readDataLen);
        }
        if (Ret != ERROR_SUCCESS)
        {
            // close registry
            RegCloseKey(hKey);
            return false;
        }
        // copy read data
        *readData = readBuffer;
        // close registry
        RegCloseKey(hKey);
        return true;
    }
    else
    {
        return false;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    // Com port
    TCHAR PortNo[] = L"COM1:";
    int Status = 0;
    PWCHAR readMessage = nullptr;

    // Read com port configuration from the registry
    Status = ReadComConfiguration(
                 HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Ports", PortNo, &readMessage);
    if (Status != TRUE)
    {
        return FALSE;
    }

    if (readMessage != nullptr)
    {
        // Display com status
        printf(" COM Status = %S\n", readMessage);
        free(readMessage);
        readMessage = nullptr;
    }
    return 0;
}

 

See the below image if you will run the above code, you will get the all the setting.

get port setting using win32 api

 

After running the code,

get port setting using win32 api

 

get port setting using win32 api

After running the code,

Get port setting win32 api