diff --git a/src/impl/list_ports/list_ports_win.cc b/src/impl/list_ports/list_ports_win.cc index dac5fd7..c6d5c0c 100644 --- a/src/impl/list_ports/list_ports_win.cc +++ b/src/impl/list_ports/list_ports_win.cc @@ -8,6 +8,7 @@ */ #include "serial/serial.h" +#include #include #include #include @@ -21,6 +22,15 @@ static const DWORD port_name_max_length = 256; static const DWORD friendly_name_max_length = 256; static const DWORD hardware_id_max_length = 256; +// Convert a wide Unicode string to an UTF8 string +std::string utf8_encode(const std::wstring &wstr) +{ + int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); + std::string strTo( size_needed, 0 ); + WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); + return strTo; +} + vector serial::list_ports() { @@ -51,16 +61,16 @@ serial::list_ports() DIREG_DEV, KEY_READ); - char port_name[port_name_max_length]; + TCHAR port_name[port_name_max_length]; DWORD port_name_length = port_name_max_length; LONG return_code = RegQueryValueEx( - hkey, - "PortName", - NULL, + hkey, + _T("PortName"), NULL, - (LPBYTE)port_name, - &port_name_length); + NULL, + (LPBYTE)port_name, + &port_name_length); RegCloseKey(hkey); @@ -74,22 +84,22 @@ serial::list_ports() // Ignore parallel ports - if(strstr(port_name, "LPT") != NULL) + if(_tcsstr(port_name, _T("LPT")) != NULL) continue; // Get port friendly name - char friendly_name[friendly_name_max_length]; + TCHAR friendly_name[friendly_name_max_length]; DWORD friendly_name_actual_length = 0; BOOL got_friendly_name = SetupDiGetDeviceRegistryProperty( - device_info_set, - &device_info_data, - SPDRP_FRIENDLYNAME, - NULL, - (PBYTE)friendly_name, - friendly_name_max_length, - &friendly_name_actual_length); + device_info_set, + &device_info_data, + SPDRP_FRIENDLYNAME, + NULL, + (PBYTE)friendly_name, + friendly_name_max_length, + &friendly_name_actual_length); if(got_friendly_name == TRUE && friendly_name_actual_length > 0) friendly_name[friendly_name_actual_length-1] = '\0'; @@ -98,27 +108,37 @@ serial::list_ports() // Get hardware ID - char hardware_id[hardware_id_max_length]; + TCHAR hardware_id[hardware_id_max_length]; DWORD hardware_id_actual_length = 0; BOOL got_hardware_id = SetupDiGetDeviceRegistryProperty( - device_info_set, - &device_info_data, - SPDRP_HARDWAREID, - NULL, - (PBYTE)hardware_id, - hardware_id_max_length, - &hardware_id_actual_length); + device_info_set, + &device_info_data, + SPDRP_HARDWAREID, + NULL, + (PBYTE)hardware_id, + hardware_id_max_length, + &hardware_id_actual_length); if(got_hardware_id == TRUE && hardware_id_actual_length > 0) hardware_id[hardware_id_actual_length-1] = '\0'; else hardware_id[0] = '\0'; + #ifdef UNICODE + std::string portName = utf8_encode(port_name); + std::string friendlyName = utf8_encode(friendly_name); + std::string hardwareId = utf8_encode(hardware_id); + #else + std::string portName = port_name; + std::string friendlyName = friendly_name; + std::string hardwareId = hardware_id; + #endif + PortInfo port_entry; - port_entry.port = port_name; - port_entry.description = friendly_name; - port_entry.hardware_id = hardware_id; + port_entry.port = portName; + port_entry.description = friendlyName; + port_entry.hardware_id = hardwareId; devices_found.push_back(port_entry); }