Retrieve USB VID:PID for devices. master
authorPat Thoyts <patthoyts@users.sourceforge.net>
Tue, 18 Aug 2020 12:32:54 +0000 (13:32 +0100)
committerPat Thoyts <patthoyts@users.sourceforge.net>
Tue, 18 Aug 2020 12:32:54 +0000 (13:32 +0100)
Updated the CMake build files.

CMakeLists.txt
lscomport.c

index d0f8ec2463ba3204e637d072d05c3d0f61125c5b..54bbd94b7193f7a078880b5584609179136ae3c3 100644 (file)
@@ -4,12 +4,13 @@
 # note - rebuild can be done also as:
 #   cmake --build . --clean-first --target demo
 
 # note - rebuild can be done also as:
 #   cmake --build . --clean-first --target demo
 
-cmake_minimum_required(VERSION 2.8)
-project(lscomport)
-add_executable(lscomport  lscomport.c)
-
-if (MSVC)
-  add_definitions(-W3 -MD -D_CRT_SECURE_NO_WARNINGS)
-else()
-  add_definitions(-std=c99 -Wall)
-endif()
+cmake_minimum_required(VERSION 3.11)
+project(lscomport VERSION 1.0)
+
+set(CMAKE_C_STANDARD 11)
+
+set(SOURCE ${PROJECT_NAME}.c)
+
+add_definitions(-DSTRICT -DWIN32_LEAN_AND_MEAN -DUNICODE -D_UNICODE)
+add_executable(${PROJECT_NAME} ${SOURCE})
+target_link_libraries(${PROJECT_NAME} "advapi32" "user32" "setupapi")
index e6589a8ee5f295ae315ba847fd8e117b384b2d78..6a105a7a94a910c84b1c307012d099a6101f1a89 100644 (file)
@@ -3,15 +3,17 @@
  * List all COM ports (esp. including USB devices)
  *
  */
  * List all COM ports (esp. including USB devices)
  *
  */
+#define _WIN32_WINNT  _WIN32_WINNT_WIN10
+#define _WIN32_IE     _WIN32_IE_IE100
 
 
-#define STRICT
-#define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <tchar.h>
 #include <windows.h>
 #include <tchar.h>
-#include <setupapi.h>
+#include <SetupAPI.h>
+#include <stdlib.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <string.h>
 
 
-#ifdef _MSC_VER
+#if _MSC_VER >= 1200
 #pragma comment(lib, "advapi32")
 #pragma comment(lib, "setupapi")
 #pragma comment(lib, "user32")
 #pragma comment(lib, "advapi32")
 #pragma comment(lib, "setupapi")
 #pragma comment(lib, "user32")
 typedef struct tagSERIALPORTINFO
 {
     int    nPortNumber;
 typedef struct tagSERIALPORTINFO
 {
     int    nPortNumber;
+    WORD vid;
+    WORD pid;
     LPTSTR pszPortName;
     LPTSTR pszFriendlyName;
     LPTSTR pszPortName;
     LPTSTR pszFriendlyName;
+    LPTSTR pszHardwareId;
     struct tagSERIALPORTINFO* next;
 } SERIALPORTINFO, *LPSERIALPORTINFO;
 
     struct tagSERIALPORTINFO* next;
 } SERIALPORTINFO, *LPSERIALPORTINFO;
 
-BOOL  GetSerialPortInfo(LPSERIALPORTINFO *ppInfo);
+/**
+ * Creates a linked list of SERIALPORTINFO items attached to the provided pointer.
+ */
+BOOL GetSerialPortInfo(LPSERIALPORTINFO *ppInfo);
 
 /* ---------------------------------------------------------------------- */
 
 int
 _tmain(int argc, TCHAR *argv[])
 {
 
 /* ---------------------------------------------------------------------- */
 
 int
 _tmain(int argc, TCHAR *argv[])
 {
-    LPSERIALPORTINFO pPortInfo = 0;
+    LPSERIALPORTINFO pPortInfo = NULL;
     GetSerialPortInfo(&pPortInfo);
     while (pPortInfo) {
         LPSERIALPORTINFO p = pPortInfo;
         pPortInfo = pPortInfo->next;
     GetSerialPortInfo(&pPortInfo);
     while (pPortInfo) {
         LPSERIALPORTINFO p = pPortInfo;
         pPortInfo = pPortInfo->next;
-        _tprintf(_T("Port %d '%s'\n"), p->nPortNumber, p->pszFriendlyName);
+        _tprintf(_T("Port %d '%s' [%04hX:%04hX] %s\n"),
+            p->nPortNumber, p->pszFriendlyName, p->vid, p->pid, p->pszHardwareId);
         HeapFree(GetProcessHeap(), 0, p->pszPortName);
         HeapFree(GetProcessHeap(), 0, p->pszFriendlyName);
         HeapFree(GetProcessHeap(), 0, p->pszPortName);
         HeapFree(GetProcessHeap(), 0, p->pszFriendlyName);
+        HeapFree(GetProcessHeap(), 0, p->pszHardwareId);
         HeapFree(GetProcessHeap(), 0, p);
     }
     return 0;
         HeapFree(GetProcessHeap(), 0, p);
     }
     return 0;
@@ -52,7 +62,7 @@ SetupEnumeratePorts()
 {
     HDEVINFO hDevices = INVALID_HANDLE_VALUE;
     DWORD dwGuids = 0;
 {
     HDEVINFO hDevices = INVALID_HANDLE_VALUE;
     DWORD dwGuids = 0;
-    BOOL br = SetupDiClassGuidsFromName(_T("Ports"), 0, 0, &dwGuids);
+    BOOL br = SetupDiClassGuidsFromName(_T("Ports"), NULL, 0, &dwGuids);
     if (dwGuids) {
         LPGUID pguids = (LPGUID)HeapAlloc(GetProcessHeap(), 0, sizeof(GUID) * dwGuids);
         if (pguids) {
     if (dwGuids) {
         LPGUID pguids = (LPGUID)HeapAlloc(GetProcessHeap(), 0, sizeof(GUID) * dwGuids);
         if (pguids) {
@@ -76,17 +86,16 @@ EndEnumeratePorts(HANDLE hDevices)
 }
 
 BOOL
 }
 
 BOOL
-GetSerialPortInfo(LPSERIALPORTINFO *ppInfo)
+GetSerialPortInfo(LPSERIALPORTINFO *pList)
 {
     HANDLE hDevices = SetupEnumeratePorts();
     BOOL br = TRUE;
 {
     HANDLE hDevices = SetupEnumeratePorts();
     BOOL br = TRUE;
-    LPSERIALPORTINFO pHead = 0, pTail = 0;
     DWORD nDevice = 0;
 
     for (nDevice = 0; br ; nDevice++)
     {
         DWORD cbData = 0;
     DWORD nDevice = 0;
 
     for (nDevice = 0; br ; nDevice++)
     {
         DWORD cbData = 0;
-        SP_DEVINFO_DATA deviceData;
+        SP_DEVINFO_DATA deviceData = { 0 };
 
         deviceData.cbSize = sizeof(SP_DEVINFO_DATA);
         br = SetupDiEnumDeviceInfo(hDevices, nDevice, &deviceData);
 
         deviceData.cbSize = sizeof(SP_DEVINFO_DATA);
         br = SetupDiEnumDeviceInfo(hDevices, nDevice, &deviceData);
@@ -96,7 +105,7 @@ GetSerialPortInfo(LPSERIALPORTINFO *ppInfo)
             HKEY hkey = SetupDiOpenDevRegKey(hDevices, &deviceData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
             if (hkey)
             {
             HKEY hkey = SetupDiOpenDevRegKey(hDevices, &deviceData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
             if (hkey)
             {
-                DWORD cbSize = 16 * sizeof(TCHAR);
+                DWORD cbSize = sizeof(sz);
                 RegQueryValueEx(hkey, _T("PortName"), NULL, NULL, (LPBYTE)sz, &cbSize);
                 RegCloseKey(hkey);
             }
                 RegQueryValueEx(hkey, _T("PortName"), NULL, NULL, (LPBYTE)sz, &cbSize);
                 RegCloseKey(hkey);
             }
@@ -104,37 +113,37 @@ GetSerialPortInfo(LPSERIALPORTINFO *ppInfo)
             if (sz[0] == _T('C') && sz[1] == _T('O') && sz[2] == _T('M'))
             {
                 LPSERIALPORTINFO pInfo =
             if (sz[0] == _T('C') && sz[1] == _T('O') && sz[2] == _T('M'))
             {
                 LPSERIALPORTINFO pInfo =
-                    (LPSERIALPORTINFO)HeapAlloc(GetProcessHeap(), 0, sizeof(SERIALPORTINFO));
+                    (LPSERIALPORTINFO)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SERIALPORTINFO));
                 pInfo->next = 0;
                 pInfo->pszPortName =
                 pInfo->next = 0;
                 pInfo->pszPortName =
-                    (LPTSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(TCHAR) * (lstrlen(sz) + 1));
+                    (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TCHAR) * ((size_t)lstrlen(sz) + 1));
                 lstrcpy(pInfo->pszPortName, sz);
                 pInfo->nPortNumber = _ttoi(&sz[3]);
 
                 lstrcpy(pInfo->pszPortName, sz);
                 pInfo->nPortNumber = _ttoi(&sz[3]);
 
-                SetupDiGetDeviceRegistryProperty(hDevices, &deviceData, SPDRP_FRIENDLYNAME,
-                                                 NULL, NULL, 0, &cbData);
+                SetupDiGetDeviceRegistryProperty(hDevices, &deviceData, SPDRP_FRIENDLYNAME, NULL, NULL, 0, &cbData);
                 if (cbData)
                 {
                 if (cbData)
                 {
-                    pInfo->pszFriendlyName =
-                        (LPTSTR)HeapAlloc(GetProcessHeap(), 0, cbData + sizeof(TCHAR));
+                    pInfo->pszFriendlyName = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, cbData + sizeof(TCHAR));
                     br = SetupDiGetDeviceRegistryProperty(hDevices, &deviceData, SPDRP_FRIENDLYNAME,
                                                       NULL, (LPBYTE)pInfo->pszFriendlyName, cbData, NULL);
                     pInfo->pszFriendlyName[cbData] = 0;
                 }
 
                     br = SetupDiGetDeviceRegistryProperty(hDevices, &deviceData, SPDRP_FRIENDLYNAME,
                                                       NULL, (LPBYTE)pInfo->pszFriendlyName, cbData, NULL);
                     pInfo->pszFriendlyName[cbData] = 0;
                 }
 
-                if (pTail == 0)
-                {
-                    pHead = pTail = pInfo;
-                }
-                else
+                // Get the hardware ID (contains the VID:PID is a USB device)
+                SetupDiGetDeviceRegistryProperty(hDevices, &deviceData, SPDRP_HARDWAREID, NULL, NULL, 0, &cbData);
+                if (cbData)
                 {
                 {
-                    pTail->next = pInfo;
-                    pTail = pInfo;
+                    pInfo->pszHardwareId = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbData + sizeof(TCHAR));
+                    br = SetupDiGetDeviceRegistryProperty(hDevices, &deviceData, SPDRP_HARDWAREID, NULL, (LPBYTE)pInfo->pszHardwareId, cbData, NULL);
+                    pInfo->pszHardwareId[cbData] = 0;
+                    _stscanf_s(pInfo->pszHardwareId, _T("USB\\VID_%04hx&PID_%04hx"), &pInfo->vid, &pInfo->pid);
                 }
                 }
+
+                *pList = pInfo;
+                pList = &(pInfo->next);
             }
         }
     }
     EndEnumeratePorts(hDevices);
             }
         }
     }
     EndEnumeratePorts(hDevices);
-    *ppInfo = pHead;
     return br;
 }
     return br;
 }