22 static const DWORD port_name_max_length = 256;
23 static const DWORD friendly_name_max_length = 256;
24 static const DWORD hardware_id_max_length = 256;
27 std::string utf8_encode(
const std::wstring &wstr)
29 int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (
int)wstr.size(), NULL, 0, NULL, NULL);
30 std::string strTo( size_needed, 0 );
31 WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (
int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
38 vector<PortInfo> devices_found;
40 HDEVINFO device_info_set = SetupDiGetClassDevs(
41 (
const GUID *) &GUID_DEVCLASS_PORTS,
46 unsigned int device_info_set_index = 0;
47 SP_DEVINFO_DATA device_info_data;
49 device_info_data.cbSize =
sizeof(SP_DEVINFO_DATA);
51 while(SetupDiEnumDeviceInfo(device_info_set, device_info_set_index, &device_info_data))
53 device_info_set_index++;
57 HKEY hkey = SetupDiOpenDevRegKey(
65 TCHAR port_name[port_name_max_length];
66 DWORD port_name_length = port_name_max_length;
68 LONG return_code = RegQueryValueEx(
78 if(return_code != EXIT_SUCCESS)
81 if(port_name_length > 0 && port_name_length <= port_name_max_length)
82 port_name[port_name_length-1] =
'\0';
88 if(_tcsstr(port_name, _T(
"LPT")) != NULL)
93 TCHAR friendly_name[friendly_name_max_length];
94 DWORD friendly_name_actual_length = 0;
96 BOOL got_friendly_name = SetupDiGetDeviceRegistryProperty(
101 (PBYTE)friendly_name,
102 friendly_name_max_length,
103 &friendly_name_actual_length);
105 if(got_friendly_name == TRUE && friendly_name_actual_length > 0)
106 friendly_name[friendly_name_actual_length-1] =
'\0';
108 friendly_name[0] =
'\0';
112 TCHAR hardware_id[hardware_id_max_length];
113 DWORD hardware_id_actual_length = 0;
115 BOOL got_hardware_id = SetupDiGetDeviceRegistryProperty(
121 hardware_id_max_length,
122 &hardware_id_actual_length);
124 if(got_hardware_id == TRUE && hardware_id_actual_length > 0)
125 hardware_id[hardware_id_actual_length-1] =
'\0';
127 hardware_id[0] =
'\0';
130 std::string portName = utf8_encode(port_name);
131 std::string friendlyName = utf8_encode(friendly_name);
132 std::string hardwareId = utf8_encode(hardware_id);
134 std::string portName = port_name;
135 std::string friendlyName = friendly_name;
136 std::string hardwareId = hardware_id;
140 port_entry.port = portName;
141 port_entry.description = friendlyName;
142 port_entry.hardware_id = hardwareId;
144 devices_found.push_back(port_entry);
147 SetupDiDestroyDeviceInfoList(device_info_set);
149 return devices_found;
152 #endif // #if defined(_WIN32)