-
Notifications
You must be signed in to change notification settings - Fork 116
bugfix: deviceName garbled in Windows #34 #86
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ _SetupDiGetDeviceRegistryProperty DllSetupDiGetDeviceRegistryProperty; | |
* Local Helper Functions protoypes | ||
**********************************/ | ||
void UpdateDevice(PDEV_BROADCAST_DEVICEINTERFACE pDevInf, WPARAM wParam, DeviceState_t state); | ||
std::string Utf8Encode(const std::string &str); | ||
DWORD WINAPI ListenerThread(LPVOID lpParam); | ||
|
||
void BuildInitialDeviceList(); | ||
|
@@ -421,14 +422,14 @@ void ExtractDeviceInfo(HDEVINFO hDevInfo, SP_DEVINFO_DATA* pspDevInfoData, TCHAR | |
|
||
// device found | ||
if (DllSetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData, SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, buffSize, &nSize)) { | ||
resultItem->deviceName = buf; | ||
resultItem->deviceName = Utf8Encode(buf); | ||
} | ||
else if ( DllSetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData, SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, buffSize, &nSize)) | ||
{ | ||
resultItem->deviceName = buf; | ||
resultItem->deviceName = Utf8Encode(buf); | ||
} | ||
if (DllSetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData, SPDRP_MFG, &DataT, (PBYTE)buf, buffSize, &nSize)) { | ||
resultItem->manufacturer = buf; | ||
resultItem->manufacturer = Utf8Encode(buf); | ||
} | ||
if (DllSetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData, SPDRP_HARDWAREID, &DataT, (PBYTE)buf, buffSize, &nSize)) { | ||
// Use this to extract VID / PID | ||
|
@@ -550,3 +551,22 @@ void UpdateDevice(PDEV_BROADCAST_DEVICEINTERFACE pDevInf, WPARAM wParam, DeviceS | |
|
||
SetEvent(deviceChangedRegisteredEvent); | ||
} | ||
|
||
std::string Utf8Encode(const std::string &str) | ||
{ | ||
if (str.empty()) { | ||
return std::string(); | ||
} | ||
|
||
//System default code page to wide character | ||
int wstr_size = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); | ||
std::wstring wstr_tmp(wstr_size, 0); | ||
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wstr_tmp[0], wstr_size); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this is pretty similar to https://stackoverflow.com/a/6693107/796832 so seems good enough to me |
||
|
||
//Wide character to Utf8 | ||
int str_size = WideCharToMultiByte(CP_UTF8, 0, &wstr_tmp[0], (int)wstr_tmp.size(), NULL, 0, NULL, NULL); | ||
std::string str_utf8(str_size, 0); | ||
WideCharToMultiByte(CP_UTF8, 0, &wstr_tmp[0], (int)wstr_tmp.size(), &str_utf8[0], str_size, NULL, NULL); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems pretty similar to WideCharToMultiByte so seems good enough to me |
||
|
||
return str_utf8; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you get this code from somewhere like StackOverflow? Let's link it for reference like the following at the top of this function // via https://stackoverflow.com/q/25410284/796832 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, i wrote it myself, it easy for me, i wrote is before. sometimes i need change the code page from 'gbk' to 'utf8'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a bit too much magic here for me to be comfortable. Just seems like something could be exploitable here with all of this reference and byte copying stuff. Would definitely prefer a standard approach to tackle this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
std::string
still available after #84 is merged?Also any of the string stuff we are doing below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem, 'std::string' is the c++ standard,mean string with namespace std,
#84 problem is the 'ATL' is not the standard library.