On the internet to find a few examples of traversing the serial port, either the code is not complete, or there is a bug, such as reading the serial number is more than 10.
After my finishing, now share the final code, VS2008 under the compilation.
This method is also useful for traversing Windows power-on startup items with just a few modifications.
void Cenumportdlg::enumport (void) {HKEY HKEY; LPCTSTR lpsubkey= "hardware\\devicemap\\serialcomm\\"; if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, Lpsubkey, 0, key_read, &hkey)! = ERROR_SUCCESS) { return;} #define Name_len -Char Szvaluename[name_len]; BYTE Szportname[name_len]; LONG status; DWORD dwindex = 0; DWORD dwsizevaluename=100; DWORD dwsizeofportname=100; DWORD Type; Dwsizevaluename = Name_len; Dwsizeofportname = Name_len; do { status = RegEnumValue (HKey, dwindex++, szValueName, &dwsizevaluename, NULL, &type, Szportname, &dwsizeofportname); if (status = = ERROR_SUCCESS)) { m_lstport.addstring ((char *) szportname); } Every read once dwsizevaluename and Dwsizeofportname will be modified //attention must be reset, or there will be a very strange error, I tried because there is no reset, there is no read COM more than 10 of the serial port Dwsizevaluename = Name_len;
Transferred from: http://www.gzzsc.net
Serial Programming-enumerate through the serial port, get all the serial port name of PC, traverse registry key, RegEnumValue usage