Enum Serial Port
Serial Port 的註冊表資訊是放在
HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
手邊有一個 USB 轉 RS232 的傳輸線,插入或移除時可觀察到註冊表的變化。
How to enum serial port ?
int EnumComPort(vector *pvec)
{
HKEY hKey;
LONG lReturn;
DWORD dwValue = 0, dwType, dwData;
DWORD dwValName;
TCHAR szValueName[256];
TCHAR szData[1024];
BOOL bIsPath;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"HARDWARE\\DEVICEMAP\\SERIALCOMM",
0, KEY_READ, &hKey) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return 0;
} else
bIsPath = TRUE;
while(bIsPath == TRUE)
{
dwData = sizeof(szData);
dwValName = sizeof(szValueName);
lReturn = RegEnumValue(hKey, dwValue++, szValueName, &dwValName,
NULL, &dwType, (UCHAR*)szData, &dwData);
if(lReturn == ERROR_NO_MORE_ITEMS)
break;
pvec->push_back(szData);
}
RegCloseKey(hKey);
return pvec->size();
}
其它方法參考
- EnumSerialPorts v1.12 A set of functions to enumerate serial ports
- CodeProject - EnumSerialPorts v1.02 - Enumerating Serial Ports
Dotblogs 的標籤: WinAPI