From d9847ff87b1ce11848eeee395ae65c77d372f2f0 Mon Sep 17 00:00:00 2001 From: Christopher Baker Date: Fri, 11 Jul 2014 09:28:55 -0500 Subject: [PATCH 1/2] Fixes to allow cross correct encoding on both codeblocsk / mingw and visual studio. --- src/impl/list_ports/list_ports_win.cc | 72 +++++++++++++++++---------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/src/impl/list_ports/list_ports_win.cc b/src/impl/list_ports/list_ports_win.cc index 4e6e0cf..e956dfe 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 // !UNICODE + 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); } From 24564c59d3a496f9577800005396f39af0e74f79 Mon Sep 17 00:00:00 2001 From: Christopher Baker Date: Sun, 13 Jul 2014 09:17:27 -0500 Subject: [PATCH 2/2] Remove confusing comment. --- src/impl/list_ports/list_ports_win.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/impl/list_ports/list_ports_win.cc b/src/impl/list_ports/list_ports_win.cc index e956dfe..47acc12 100644 --- a/src/impl/list_ports/list_ports_win.cc +++ b/src/impl/list_ports/list_ports_win.cc @@ -133,7 +133,7 @@ serial::list_ports() std::string portName = port_name; std::string friendlyName = friendly_name; std::string hardwareId = hardware_id; - #endif // !UNICODE + #endif PortInfo port_entry; port_entry.port = portName;