2023年8月25日 星期五

List all serial com port on Win10

Copied the code from Here
#define WIN32_LEAN_AND_MEAN  // excludes stuff frokm windows.h that we won't need here.
#include  <Windows.h>
#include  <string.h>
#include  <tchar.h>
#include  <malloc.h>
#include  <stdio.h>

void ShowErrorFromLStatus(LSTATUS lResult)
{
    LPTSTR psz;
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        lResult,
        0,
        (LPTSTR)&psz,
        1024,
        NULL);

    _tprintf(_T("Windows reports error: (0x%08X): %s\n"), lResult, (psz) ? psz : _T("(null)"));
    if (psz)
    {
        LocalFree(psz);
    }
}

int main()
{
    DWORD nValues, nMaxValueNameLen, nMaxValueLen;
    HKEY hKey = NULL;
    LPTSTR szDeviceName = NULL;
    LPTSTR szFriendlyName = NULL;
    DWORD dwType = 0;
    DWORD nValueNameLen = 0;
    DWORD nValueLen = 0;
    DWORD dwIndex = 0;

    LSTATUS lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_READ, &hKey);
    if (ERROR_SUCCESS != lResult)
    {
        printf("Failed to open key \'HARDWARE\\DEVICEMAP\\SERIALCOMM\' \n");
        ShowErrorFromLStatus(lResult);
        return 1;
    }

    lResult = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
        &nValues, &nMaxValueNameLen, &nMaxValueLen, NULL, NULL);

    if (ERROR_SUCCESS != lResult)
    {
        _tprintf(_T("Failed to RegQueryInfoKey()\n"));
        ShowErrorFromLStatus(lResult);
        RegCloseKey(hKey);
        return 2;
    }

    szDeviceName = (LPTSTR)malloc(nMaxValueNameLen + sizeof(TCHAR));
    if (!szDeviceName)
    {
        _tprintf(_T("malloc() fail\n"));
        RegCloseKey(hKey);
        return 3;
    }

    szFriendlyName = (LPTSTR)malloc(nMaxValueLen + sizeof(TCHAR));
    if (!szFriendlyName)
    {
        free(szDeviceName);
        _tprintf(_T("malloc() fail\n"));
        RegCloseKey(hKey);
        return 3;
    }

    _tprintf(_T("Found %d serial device(s) registered with PnP and active or available at the moment.\n"), nValues);

    for (DWORD dwIndex = 0; dwIndex  &lt; nValues; ++dwIndex)
    {
        dwType = 0;
        nValueNameLen = nMaxValueNameLen + sizeof(TCHAR);
        nValueLen = nMaxValueLen + sizeof(TCHAR);

        lResult = RegEnumValueW(hKey, dwIndex, 
            (LPWSTR)szDeviceName, &nValueNameLen,
            NULL, &dwType, 
            (LPBYTE)szFriendlyName, &nValueLen);

        if (ERROR_SUCCESS != lResult || REG_SZ != dwType)
        {
            _tprintf(_T("SerialPortEnumerator::Init() : can't process registry value, index: %d\n"), dwIndex);
            ShowErrorFromLStatus(lResult);
            continue;
        }
        _tprintf(_T("Found port \'%ls\': Device name for CreateFile(): \'\\.%ls\'\n"), szFriendlyName, szDeviceName);
    }

    free(szDeviceName);
    free(szFriendlyName);
    RegCloseKey(hKey);
    return 0;
}

Output:
C:\Test>auto_com_port.exe
Found 8 serial device(s) registered with PnP and active or available at the moment.
Found port 'COM1': Device name for CreateFile(): '\.\Device\Serial0'
Found port 'COM24': Device name for CreateFile(): '\.\Device\USBSER00COM24'
Found port 'COM25': Device name for CreateFile(): '\.\Device\USBSER00COM25'
Found port 'COM22': Device name for CreateFile(): '\.\Device\USBSER00COM22'
Found port 'COM23': Device name for CreateFile(): '\.\Device\USBSER00COM23'
Found port 'COM15': Device name for CreateFile(): '\.\Device\QCUSB_COCOM15'
Found port 'COM16': Device name for CreateFile(): '\.\Device\QCUSB_COCOM16'
Found port 'COM17': Device name for CreateFile(): '\.\Device\QCUSB_COCOM17'


ref:
Here

沒有留言:

張貼留言