Automatic Identification of serial Registry
Automatic Identification of Windows serial port registry:
How to open the Windows registry:
Enter "regedit" at run to open the Registry
Struct UartInfo {DWORD UartNum; char UartName [20] ;}; // get the serial port list BOOL EnumComs (struct UartInfo ** UartCom, LPDWORD UartComNumber) {// LPCTSTR: const char ** UartComNumber = 0; const char subKey [100] = "HARDWARE \ DEVICEMAP \ SERIALCOMM"; HKEY hNewKey; LONG lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, subKey, 0, KEY_ALL_ACCESS, & hNewKey); if (lResult! = ERROR_SUCCESS) {PrintLog ("An error occurred while opening the COM registry !!! "); Return FALSE;} else {PrintLog (" COM registry opened successfully !!! ") ;}// DWORD: unsigned longDWORD ValuesNumber; DWORD MaxValueNameLen; DWORD MaxValueLen; CString str; // retrieve the number of value items under the specified sub-key lResult = RegQueryInfoKey (hNewKey, NULL, & ValuesNumber, & MaxValueNameLen, & MaxValueLen, NULL, NULL); if (lResult! = ERROR_SUCCESS) {RegCloseKey (hNewKey); PrintLog ("retrieving the number of serial ports connected to the PC failed !!! "); Return FALSE;} else {str. format ("the number of serial ports connected to the PC is % ld", ValuesNumber); PrintLog (str); * UartCom = (struct UartInfo *) malloc (ValuesNumber * sizeof (struct UartInfo);} DWORD index; DWORD uartindex = 0; // CHAR ValueName [MAX_VALUE_NAME]; CHAR ValueName [100]; // DWORD ValueNameSize = MAX_VALUE_NAME; DWORD ValueNameSize; DWORD DataType; BYTE DataBuffer [100] = ""; DWORD DataLen = 100; // LPTSTR is char *, L PBYTE is char * // retrieves each value item and obtains the Value Name, data type, data for (index = 0; index <ValuesNumber; index ++) {memset (ValueName, 0, sizeof (ValueName); memset (DataBuffer, 0, sizeof (DataBuffer); ValueNameSize = 100; DataLen = 100; lResult = RegEnumValue (hNewKey, index, ValueName, & ValueNameSize, NULL, & DataType, DataBuffer, & DataLen); if (lResult = ERROR_SUCCESS) {switch (DataType) {case REG_NONE: // No value type (0) // Print Log ("DataType: REG_NONE"); break; case REG_SZ: // Unicode nul terminated string (1) // PrintLog ("DataType: REG_SZ"); break; case REG_EXPAND_SZ: // Unicode nul terminated string (2) // PrintLog ("DataType: REG_EXPAND_SZ"); break; case REG_BINARY: // Free form binary (3) // PrintLog ("DataType: REG_BINARY "); break; case REG_DWORD: // 32-bit number (4) // PrintLog (" DataType: REG_DWORD "); break; case REG_MULTI_SZ: // Multiple Unicode strings (7) // PrintLog ("DataType: REG_MULTI_SZ"); break; default: // PrintLog ("Other DataType !!! "); Break;} # if 0 str. format ("value: % s", ValueName); PrintLog (str); str. format ("Value Name Length: % ld", ValueNameSize); PrintLog (str); str. format ("data: % s", DataBuffer); PrintLog (str); str. format ("Data Length: % ld", DataLen); PrintLog (str); # endif memcpy (* UartCom) [uartindex]. uartName, DataBuffer, DataLen); (* UartCom) [uartindex]. uartNum = ValuesNumber; uartindex ++;} else if (lResult = ERROR_NO_MORE_ITEMS) {PrintLog ("Serial Port retrieval completed !!! ");} Else {DWORD dw = GetLastError (); str. format ("Serial retrieval error: 0x % 08x", dw); PrintLog (str); return FALSE ;}} * UartComNumber = uartindex; return TRUE ;}
Code called in the main function:
{
DWORD UartComNumber = 0; struct UartInfo * pUartCom; BOOL bResult; bResult = EnumComs (& pUartCom, & UartComNumber); DWORD index; if (bResult) {PrintLog ("succeeded in obtaining the serial port List");} else {PrintLog ("failed to get the serial port List");} for (index = 0; index <UartComNumber; index ++) {pCDlg-> m_combox.addstring (pUartCom [index]. uartName);} if (pUartCom! = NULL) {free (pUartCom); pUartCom = NULL;} ResetEvent (g_UartEvent); g_UartEvent = CreateEvent (NULL, TRUE, TRUE, NULL); // event handle for asynchronous read/write
}