Enum Serial Port

Enum Serial Port

Serial Port 的註冊表資訊是放在

        HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM

 

image

手邊有一個 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();
}

其它方法參考

 

Dotblogs 的標籤: