#include <Windows.h>
#include <SetupAPI.h> // -lsetupapi
#include <devguid.h> // -luuid
#include <stdio.h>
#include <map>
using namespace std;
map<string, string> get_serial_ports()
{
map<string, string> port_name_to_dev_name;
HDEVINFO h_dev_info = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, NULL, NULL, DIGCF_PRESENT);
if (h_dev_info == INVALID_HANDLE_VALUE)
{
return port_name_to_dev_name;
}
SP_DEVINFO_DATA dev_info_data;
dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInfo(h_dev_info, i, &dev_info_data); i++)
{
HKEY h_key_dev = SetupDiOpenDevRegKey(h_dev_info, &dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
DWORD type;
DWORD size = 256; // size会被RegQueryValueEx修改
char port_name[256];
char dev_name[256];
if (h_key_dev != INVALID_HANDLE_VALUE
&& RegQueryValueEx(h_key_dev, "PortName", NULL, &type, (LPBYTE)port_name, &size) == ERROR_SUCCESS
&& type == REG_SZ
&& SetupDiGetDeviceRegistryProperty(h_dev_info, &dev_info_data, SPDRP_DEVICEDESC, NULL, (PBYTE)dev_name, sizeof(dev_name), NULL))
{
port_name_to_dev_name[port_name] = dev_name;
}
if (h_key_dev != INVALID_HANDLE_VALUE)
{
RegCloseKey(h_key_dev);
}
}
SetupDiDestroyDeviceInfoList(h_dev_info);
return port_name_to_dev_name;
}
int main()
{
for (const auto& kv : get_serial_ports())
{
printf("%s\t\%s\n", kv.first.c_str(), kv.second.c_str());
}
}
标签:info,十万个,Windows,dev,key,NULL,口号,port,name From: https://www.cnblogs.com/kehuadong/p/18343590