原来C++Builder6在XP下获取串口的方法:
void TTools::GetSystemPortList(TStringList * pList) { TRegistry *regkey=new TRegistry(); pList->Clear(); regkey->RootKey=HKEY_LOCAL_MACHINE; TStringList *KeyNames; KeyNames=new TStringList(); regkey->OpenKey("HARDWARE\\DEVICEMAP\\SERIALCOMM",false); regkey->GetValueNames(KeyNames); for(int i=0;i<=(KeyNames->Count-1);i++) { AnsiString a = regkey->ReadString(KeyNames->Strings[i]); pList->Add(a); } regkey->CloseKey(); delete KeyNames; delete regkey; }
这种方法,编译好的程序,在Win10下无效了.在Win10直接用API读注册表获取
void TTools::GetSystemAllPort(TStringList * pList) { HKEY hKey; DWORD result; TCHAR valueName[255]; TCHAR data[255]; DWORD valueNameSize,dataSize; DWORD i; result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Hardware\\DeviceMap\\SerialComm", NULL, KEY_READ, &hKey);//读取注册表串口信息 if(ERROR_SUCCESS == result) { for(i=0;i<256 ;i++) { valueNameSize = dataSize = sizeof(valueName) / sizeof(TCHAR); result = RegEnumValue(hKey, i, valueName, &valueNameSize, NULL, NULL, (LPBYTE)data, &dataSize); if(ERROR_NO_MORE_ITEMS == result ) { break; } else { pList->Add((char*)data); } } } else { ::MessageBox(0,"PORT ERROR!","Error",0); } RegCloseKey(hKey); }
原文地址:http://blog.51cto.com/9233403/2067175
时间: 2024-10-19 14:54:57