mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-22 11:44:53 +08:00
Fixes to allow cross correct encoding on both codeblocsk / mingw and visual studio.
This commit is contained in:
parent
46802a9b3c
commit
d9847ff87b
@ -8,6 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "serial/serial.h"
|
#include "serial/serial.h"
|
||||||
|
#include <tchar.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <SetupAPI.h>
|
#include <SetupAPI.h>
|
||||||
#include <devguid.h>
|
#include <devguid.h>
|
||||||
@ -21,6 +22,15 @@ static const DWORD port_name_max_length = 256;
|
|||||||
static const DWORD friendly_name_max_length = 256;
|
static const DWORD friendly_name_max_length = 256;
|
||||||
static const DWORD hardware_id_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<PortInfo>
|
vector<PortInfo>
|
||||||
serial::list_ports()
|
serial::list_ports()
|
||||||
{
|
{
|
||||||
@ -51,16 +61,16 @@ serial::list_ports()
|
|||||||
DIREG_DEV,
|
DIREG_DEV,
|
||||||
KEY_READ);
|
KEY_READ);
|
||||||
|
|
||||||
char port_name[port_name_max_length];
|
TCHAR port_name[port_name_max_length];
|
||||||
DWORD port_name_length = port_name_max_length;
|
DWORD port_name_length = port_name_max_length;
|
||||||
|
|
||||||
LONG return_code = RegQueryValueEx(
|
LONG return_code = RegQueryValueEx(
|
||||||
hkey,
|
hkey,
|
||||||
"PortName",
|
_T("PortName"),
|
||||||
NULL,
|
|
||||||
NULL,
|
NULL,
|
||||||
(LPBYTE)port_name,
|
NULL,
|
||||||
&port_name_length);
|
(LPBYTE)port_name,
|
||||||
|
&port_name_length);
|
||||||
|
|
||||||
RegCloseKey(hkey);
|
RegCloseKey(hkey);
|
||||||
|
|
||||||
@ -74,22 +84,22 @@ serial::list_ports()
|
|||||||
|
|
||||||
// Ignore parallel ports
|
// Ignore parallel ports
|
||||||
|
|
||||||
if(strstr(port_name, "LPT") != NULL)
|
if(_tcsstr(port_name, _T("LPT")) != NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Get port friendly name
|
// Get port friendly name
|
||||||
|
|
||||||
char friendly_name[friendly_name_max_length];
|
TCHAR friendly_name[friendly_name_max_length];
|
||||||
DWORD friendly_name_actual_length = 0;
|
DWORD friendly_name_actual_length = 0;
|
||||||
|
|
||||||
BOOL got_friendly_name = SetupDiGetDeviceRegistryProperty(
|
BOOL got_friendly_name = SetupDiGetDeviceRegistryProperty(
|
||||||
device_info_set,
|
device_info_set,
|
||||||
&device_info_data,
|
&device_info_data,
|
||||||
SPDRP_FRIENDLYNAME,
|
SPDRP_FRIENDLYNAME,
|
||||||
NULL,
|
NULL,
|
||||||
(PBYTE)friendly_name,
|
(PBYTE)friendly_name,
|
||||||
friendly_name_max_length,
|
friendly_name_max_length,
|
||||||
&friendly_name_actual_length);
|
&friendly_name_actual_length);
|
||||||
|
|
||||||
if(got_friendly_name == TRUE && friendly_name_actual_length > 0)
|
if(got_friendly_name == TRUE && friendly_name_actual_length > 0)
|
||||||
friendly_name[friendly_name_actual_length-1] = '\0';
|
friendly_name[friendly_name_actual_length-1] = '\0';
|
||||||
@ -98,27 +108,37 @@ serial::list_ports()
|
|||||||
|
|
||||||
// Get hardware ID
|
// Get hardware ID
|
||||||
|
|
||||||
char hardware_id[hardware_id_max_length];
|
TCHAR hardware_id[hardware_id_max_length];
|
||||||
DWORD hardware_id_actual_length = 0;
|
DWORD hardware_id_actual_length = 0;
|
||||||
|
|
||||||
BOOL got_hardware_id = SetupDiGetDeviceRegistryProperty(
|
BOOL got_hardware_id = SetupDiGetDeviceRegistryProperty(
|
||||||
device_info_set,
|
device_info_set,
|
||||||
&device_info_data,
|
&device_info_data,
|
||||||
SPDRP_HARDWAREID,
|
SPDRP_HARDWAREID,
|
||||||
NULL,
|
NULL,
|
||||||
(PBYTE)hardware_id,
|
(PBYTE)hardware_id,
|
||||||
hardware_id_max_length,
|
hardware_id_max_length,
|
||||||
&hardware_id_actual_length);
|
&hardware_id_actual_length);
|
||||||
|
|
||||||
if(got_hardware_id == TRUE && hardware_id_actual_length > 0)
|
if(got_hardware_id == TRUE && hardware_id_actual_length > 0)
|
||||||
hardware_id[hardware_id_actual_length-1] = '\0';
|
hardware_id[hardware_id_actual_length-1] = '\0';
|
||||||
else
|
else
|
||||||
hardware_id[0] = '\0';
|
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;
|
PortInfo port_entry;
|
||||||
port_entry.port = port_name;
|
port_entry.port = portName;
|
||||||
port_entry.description = friendly_name;
|
port_entry.description = friendlyName;
|
||||||
port_entry.hardware_id = hardware_id;
|
port_entry.hardware_id = hardwareId;
|
||||||
|
|
||||||
devices_found.push_back(port_entry);
|
devices_found.push_back(port_entry);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user