Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

bugfix: deviceName garbled in Windows #34 #86

Merged
merged 1 commit into from
Oct 6, 2019

Conversation

nononoone
Copy link
Contributor

@nononoone nononoone commented Jun 21, 2019

Fix #34

Fix deviceName garbled in Windows

WideCharToMultiByte(CP_UTF8, 0, &wstr_tmp[0], (int)wstr_tmp.size(), &str_utf8[0], str_size, NULL, NULL);

return str_utf8;
}
Copy link
Owner

@MadLittleMods MadLittleMods Jun 22, 2019

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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'.

Copy link
Owner

Choose a reason for hiding this comment

The 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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -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);
Copy link
Owner

@MadLittleMods MadLittleMods Jun 22, 2019

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?

Copy link
Contributor Author

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.

@MadLittleMods
Copy link
Owner

MadLittleMods commented Jun 22, 2019

@nononoone Can you give a before after results when you use usbDetect.find?

@nononoone
Copy link
Contributor Author

@nononoone Can you give a before after results when you use usbDetect.find?

before:

G:\Test\node_modules\usb-detection\test>node dev-helper-test.js
startMonitoring
find 14 [ { locationId: 0,
    vendorId: 0,
    productId: 0,
    deviceName: 'USB Root Hub',
    manufacturer: '(��׼ USB ��������)',
    serialNumber: '',
    deviceAddress: 12 },
  { locationId: 0,
    vendorId: 0,
    productId: 0,
    deviceName: 'USB Root Hub',
    manufacturer: '(��׼ USB ��������)',
    serialNumber: '',
    deviceAddress: 3 },
  { locationId: 0,
    vendorId: 0,
    productId: 0,
    deviceName: 'USB ��������(USB 3.0)',
    manufacturer: '(��׼ USB ������)',
    serialNumber: '',
    deviceAddress: 10 },

after:

G:\Working\Test\node_modules\usb-detection\test>node dev-helper-test.js  
startMonitoring
find 14 [ { locationId: 0,
    vendorId: 0,
    productId: 0,
    deviceName: 'USB Root Hub',
    manufacturer: '(标准 USB 主控制器)',
    serialNumber: '',
    deviceAddress: 12 },
  { locationId: 0,
    vendorId: 0,
    productId: 0,
    deviceName: 'USB Root Hub',
    manufacturer: '(标准 USB 主控制器)',
    serialNumber: '',
    deviceAddress: 3 },
  { locationId: 0,
    vendorId: 0,
    productId: 0,
    deviceName: 'USB 根集线器(USB 3.0)',
    manufacturer: '(标准 USB 集线器)',
    serialNumber: '',
    deviceAddress: 10 },

@ilopsource
Copy link

V4.3.0 encountered the same bug, I hope to be able to merge this code in the new version,please

//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);
Copy link
Owner

Choose a reason for hiding this comment

The 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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems pretty similar to WideCharToMultiByte so seems good enough to me

@MadLittleMods MadLittleMods merged commit 1b78855 into MadLittleMods:master Oct 6, 2019
MadLittleMods added a commit that referenced this pull request Oct 6, 2019
@MadLittleMods
Copy link
Owner

Sorry for the delay @nononoone 🥶

This is now shipped in usb-detection@4.4.0 🎉

Thanks for the fix ❤️

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

deviceName garbled in Windows
3 participants