@serialport+bindings-cpp+11.0.1.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. diff --git a/node_modules/@serialport/bindings-cpp/src/serialport_win.cpp b/node_modules/@serialport/bindings-cpp/src/serialport_win.cpp
  2. index b11c07f..910023e 100644
  3. --- a/node_modules/@serialport/bindings-cpp/src/serialport_win.cpp
  4. +++ b/node_modules/@serialport/bindings-cpp/src/serialport_win.cpp
  5. @@ -10,6 +10,7 @@
  6. #include <initguid.h>
  7. #include <devpkey.h>
  8. #include <devguid.h>
  9. +#include <wchar.h>
  10. #pragma comment(lib, "setupapi.lib")
  11. #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
  12. @@ -25,6 +26,29 @@ typedef BOOL (WINAPI *CancelIoExType)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
  13. std::list<int> g_closingHandles;
  14. +void ErrorCodeToString(const wchar_t* prefix, int errorCode, wchar_t *errorStr) {
  15. + switch (errorCode) {
  16. + case ERROR_FILE_NOT_FOUND:
  17. + _snwprintf_s(errorStr, ERROR_STRING_SIZE, _TRUNCATE, L"%ls: File not found", prefix);
  18. + break;
  19. + case ERROR_INVALID_HANDLE:
  20. + _snwprintf_s(errorStr, ERROR_STRING_SIZE, _TRUNCATE, L"%ls: Invalid handle", prefix);
  21. + break;
  22. + case ERROR_ACCESS_DENIED:
  23. + _snwprintf_s(errorStr, ERROR_STRING_SIZE, _TRUNCATE, L"%ls: Access denied", prefix);
  24. + break;
  25. + case ERROR_OPERATION_ABORTED:
  26. + _snwprintf_s(errorStr, ERROR_STRING_SIZE, _TRUNCATE, L"%ls: Operation aborted", prefix);
  27. + break;
  28. + case ERROR_INVALID_PARAMETER:
  29. + _snwprintf_s(errorStr, ERROR_STRING_SIZE, _TRUNCATE, L"%ls: The parameter is incorrect %d", prefix, errorCode);
  30. + break;
  31. + default:
  32. + _snwprintf_s(errorStr, ERROR_STRING_SIZE, _TRUNCATE, L"%ls: Unknown error code %d", prefix, errorCode);
  33. + break;
  34. + }
  35. +}
  36. +
  37. void ErrorCodeToString(const char* prefix, int errorCode, char *errorStr) {
  38. switch (errorCode) {
  39. case ERROR_FILE_NOT_FOUND:
  40. @@ -608,9 +632,9 @@ void CloseBaton::Execute() {
  41. }
  42. }
  43. -char *copySubstring(char *someString, int n) {
  44. - char *new_ = reinterpret_cast<char*>(malloc(sizeof(char)*n + 1));
  45. - strncpy_s(new_, n + 1, someString, n);
  46. +wchar_t *copySubstring(wchar_t *someString, int n) {
  47. + wchar_t *new_ = reinterpret_cast<wchar_t*>(malloc(sizeof(wchar_t)*n + 1));
  48. + wcsncpy_s(new_, n + 1, someString, n);
  49. new_[n] = '\0';
  50. return new_;
  51. }
  52. @@ -625,7 +649,7 @@ Napi::Value List(const Napi::CallbackInfo& info) {
  53. Napi::Function callback = info[0].As<Napi::Function>();
  54. ListBaton* baton = new ListBaton(callback);
  55. - snprintf(baton->errorString, sizeof(baton->errorString), "");
  56. + _snwprintf(baton->errorString, sizeof(baton->errorString), L"");
  57. baton->Queue();
  58. return env.Undefined();
  59. @@ -633,20 +657,20 @@ Napi::Value List(const Napi::CallbackInfo& info) {
  60. // It's possible that the s/n is a construct and not the s/n of the parent USB
  61. // composite device. This performs some convoluted registry lookups to fetch the USB s/n.
  62. -void getSerialNumber(const char *vid,
  63. - const char *pid,
  64. +void getSerialNumber(const wchar_t *vid,
  65. + const wchar_t *pid,
  66. const HDEVINFO hDevInfo,
  67. SP_DEVINFO_DATA deviceInfoData,
  68. const unsigned int maxSerialNumberLength,
  69. - char* serialNumber) {
  70. - _snprintf_s(serialNumber, maxSerialNumberLength, _TRUNCATE, "");
  71. + wchar_t* serialNumber) {
  72. + _snwprintf_s(serialNumber, maxSerialNumberLength, _TRUNCATE, L"");
  73. if (vid == NULL || pid == NULL) {
  74. return;
  75. }
  76. DWORD dwSize;
  77. WCHAR szWUuidBuffer[MAX_BUFFER_SIZE];
  78. - WCHAR containerUuid[MAX_BUFFER_SIZE];
  79. + WCHAR wantedUuid[MAX_BUFFER_SIZE];
  80. // Fetch the "Container ID" for this device node. In USB context, this "Container
  81. @@ -683,7 +707,7 @@ void getSerialNumber(const char *vid,
  82. // Given the UUID bytes, build up a (widechar) string from it. There's some mangling
  83. // going on.
  84. - StringFromGUID2((REFGUID)szWUuidBuffer, containerUuid, ARRAY_SIZE(containerUuid));
  85. + StringFromGUID2((REFGUID)szWUuidBuffer, wantedUuid, ARRAY_SIZE(wantedUuid));
  86. } else {
  87. // Container UUID could not be fetched, return empty serial number.
  88. return;
  89. @@ -693,21 +717,15 @@ void getSerialNumber(const char *vid,
  90. // This means they're non-removable, and are not handled (yet).
  91. // Maybe they should inherit the s/n from somewhere else.
  92. - // Sanitize input - for whatever reason, StringFromGUID2() returns a WCHAR* but
  93. - // the comparisons later need a plain old char*, in lowercase ASCII.
  94. - char wantedUuid[MAX_BUFFER_SIZE];
  95. - _snprintf_s(wantedUuid, MAX_BUFFER_SIZE, _TRUNCATE, "%ws", containerUuid);
  96. - strlwr(wantedUuid);
  97. -
  98. // Iterate through all the USB devices with the given VendorID/ProductID
  99. HKEY vendorProductHKey;
  100. DWORD retCode;
  101. - char hkeyPath[MAX_BUFFER_SIZE];
  102. + wchar_t hkeyPath[MAX_BUFFER_SIZE];
  103. - _snprintf_s(hkeyPath, MAX_BUFFER_SIZE, _TRUNCATE, "SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_%s&PID_%s", vid, pid);
  104. + _snwprintf_s(hkeyPath, MAX_BUFFER_SIZE, _TRUNCATE, L"SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_%s&PID_%s", vid, pid);
  105. - retCode = RegOpenKeyEx(
  106. + retCode = RegOpenKeyExW(
  107. HKEY_LOCAL_MACHINE,
  108. hkeyPath,
  109. 0,
  110. @@ -739,9 +757,9 @@ void getSerialNumber(const char *vid,
  111. // Each of the subkeys here is the serial number of a USB device with the
  112. // given VendorId/ProductId. Now fetch the string for the S/N.
  113. DWORD serialNumberLength = maxSerialNumberLength;
  114. - retCode = RegEnumKeyEx(vendorProductHKey,
  115. + retCode = RegEnumKeyExW(vendorProductHKey,
  116. i,
  117. - serialNumber,
  118. + reinterpret_cast<LPWSTR>(serialNumber),
  119. &serialNumberLength,
  120. NULL,
  121. NULL,
  122. @@ -751,21 +769,21 @@ void getSerialNumber(const char *vid,
  123. if (retCode == ERROR_SUCCESS) {
  124. // Lookup info for VID_(vendorId)&PID_(productId)\(serialnumber)
  125. - _snprintf_s(hkeyPath, MAX_BUFFER_SIZE, _TRUNCATE,
  126. - "SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_%s&PID_%s\\%s",
  127. + _snwprintf_s(hkeyPath, MAX_BUFFER_SIZE, _TRUNCATE,
  128. + L"SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_%ls&PID_%ls\\%ls",
  129. vid, pid, serialNumber);
  130. HKEY deviceHKey;
  131. - if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, hkeyPath, 0, KEY_READ, &deviceHKey) == ERROR_SUCCESS) {
  132. - char readUuid[MAX_BUFFER_SIZE];
  133. + if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, hkeyPath, 0, KEY_READ, &deviceHKey) == ERROR_SUCCESS) {
  134. + wchar_t readUuid[MAX_BUFFER_SIZE];
  135. DWORD readSize = sizeof(readUuid);
  136. // Query VID_(vendorId)&PID_(productId)\(serialnumber)\ContainerID
  137. - retCode = RegQueryValueEx(deviceHKey, "ContainerID", NULL, NULL, (LPBYTE)&readUuid, &readSize);
  138. + retCode = RegQueryValueExW(deviceHKey, L"ContainerID", NULL, NULL, (LPBYTE)&readUuid, &readSize);
  139. if (retCode == ERROR_SUCCESS) {
  140. readUuid[readSize] = '\0';
  141. - if (strcmp(wantedUuid, readUuid) == 0) {
  142. + if (wcscmp(wantedUuid, readUuid) == 0) {
  143. // The ContainerID UUIDs match, return now that serialNumber has
  144. // the right value.
  145. RegCloseKey(deviceHKey);
  146. @@ -783,7 +801,7 @@ void getSerialNumber(const char *vid,
  147. RegCloseKey(vendorProductHKey);
  148. }
  149. - _snprintf_s(serialNumber, maxSerialNumberLength, _TRUNCATE, "");
  150. + _snwprintf_s(serialNumber, maxSerialNumberLength, _TRUNCATE, L"");
  151. return;
  152. }
  153. @@ -795,15 +813,15 @@ void ListBaton::Execute() {
  154. int memberIndex = 0;
  155. DWORD dwSize, dwPropertyRegDataType;
  156. - char szBuffer[MAX_BUFFER_SIZE];
  157. - char *pnpId;
  158. - char *vendorId;
  159. - char *productId;
  160. - char *name;
  161. - char *manufacturer;
  162. - char *locationId;
  163. - char *friendlyName;
  164. - char serialNumber[MAX_REGISTRY_KEY_SIZE];
  165. + wchar_t szBuffer[MAX_BUFFER_SIZE];
  166. + wchar_t *pnpId;
  167. + wchar_t *vendorId;
  168. + wchar_t *productId;
  169. + wchar_t *name;
  170. + wchar_t *manufacturer;
  171. + wchar_t *locationId;
  172. + wchar_t *friendlyName;
  173. + wchar_t serialNumber[MAX_REGISTRY_KEY_SIZE];
  174. bool isCom;
  175. while (true) {
  176. isCom = false;
  177. @@ -814,7 +832,6 @@ void ListBaton::Execute() {
  178. manufacturer = NULL;
  179. locationId = NULL;
  180. friendlyName = NULL;
  181. - isCom = false;
  182. ZeroMemory(&deviceInfoData, sizeof(SP_DEVINFO_DATA));
  183. deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  184. @@ -826,16 +843,16 @@ void ListBaton::Execute() {
  185. }
  186. dwSize = sizeof(szBuffer);
  187. - SetupDiGetDeviceInstanceId(hDevInfo, &deviceInfoData, szBuffer, dwSize, &dwSize);
  188. + SetupDiGetDeviceInstanceIdW(hDevInfo, &deviceInfoData, reinterpret_cast<PWSTR>(szBuffer), dwSize, &dwSize);
  189. szBuffer[dwSize] = '\0';
  190. - pnpId = strdup(szBuffer);
  191. + pnpId = wcsdup(szBuffer);
  192. - vendorId = strstr(szBuffer, "VID_");
  193. + vendorId = wcsstr(szBuffer, L"VID_");
  194. if (vendorId) {
  195. vendorId += 4;
  196. vendorId = copySubstring(vendorId, 4);
  197. }
  198. - productId = strstr(szBuffer, "PID_");
  199. + productId = wcsstr(szBuffer, L"PID_");
  200. if (productId) {
  201. productId += 4;
  202. productId = copySubstring(productId, 4);
  203. @@ -843,32 +860,29 @@ void ListBaton::Execute() {
  204. getSerialNumber(vendorId, productId, hDevInfo, deviceInfoData, MAX_REGISTRY_KEY_SIZE, serialNumber);
  205. - if (SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData,
  206. + if (SetupDiGetDeviceRegistryPropertyW(hDevInfo, &deviceInfoData,
  207. SPDRP_LOCATION_INFORMATION, &dwPropertyRegDataType,
  208. - reinterpret_cast<BYTE*>(szBuffer),
  209. - sizeof(szBuffer), &dwSize)) {
  210. - locationId = strdup(szBuffer);
  211. + reinterpret_cast<PBYTE>(szBuffer), sizeof(szBuffer), &dwSize)) {
  212. + locationId = wcsdup(szBuffer);
  213. }
  214. - if (SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData,
  215. + if (SetupDiGetDeviceRegistryPropertyW(hDevInfo, &deviceInfoData,
  216. SPDRP_FRIENDLYNAME, &dwPropertyRegDataType,
  217. - reinterpret_cast<BYTE*>(szBuffer),
  218. - sizeof(szBuffer), &dwSize)) {
  219. - friendlyName = strdup(szBuffer);
  220. + reinterpret_cast<PBYTE>(szBuffer), sizeof(szBuffer), &dwSize)) {
  221. + friendlyName = wcsdup(szBuffer);
  222. }
  223. - if (SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData,
  224. + if (SetupDiGetDeviceRegistryPropertyW(hDevInfo, &deviceInfoData,
  225. SPDRP_MFG, &dwPropertyRegDataType,
  226. - reinterpret_cast<BYTE*>(szBuffer),
  227. - sizeof(szBuffer), &dwSize)) {
  228. - manufacturer = strdup(szBuffer);
  229. + reinterpret_cast<PBYTE>(szBuffer), sizeof(szBuffer), &dwSize)) {
  230. + manufacturer = wcsdup(szBuffer);
  231. }
  232. HKEY hkey = SetupDiOpenDevRegKey(hDevInfo, &deviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
  233. if (hkey != INVALID_HANDLE_VALUE) {
  234. dwSize = sizeof(szBuffer);
  235. - if (RegQueryValueEx(hkey, "PortName", NULL, NULL, (LPBYTE)&szBuffer, &dwSize) == ERROR_SUCCESS) {
  236. + if (RegQueryValueExW(hkey, L"PortName", NULL, NULL, (LPBYTE)&szBuffer, &dwSize) == ERROR_SUCCESS) {
  237. + name = wcsdup(szBuffer);
  238. szBuffer[dwSize] = '\0';
  239. - name = strdup(szBuffer);
  240. - isCom = strstr(szBuffer, "COM") != NULL;
  241. + isCom = wcsstr(szBuffer, L"COM") != NULL;
  242. }
  243. }
  244. if (isCom) {
  245. @@ -916,6 +930,16 @@ void setIfNotEmpty(Napi::Object item, std::string key, const char *value) {
  246. }
  247. }
  248. +void setIfNotEmpty(Napi::Object item, std::string key, const wchar_t *value) {
  249. + Napi::Env env = item.Env();
  250. + Napi::String v8key = Napi::String::New(env, key);
  251. + if (wcslen(value) > 0) {
  252. + (item).Set(v8key, Napi::String::New(env, (const char16_t*) value));
  253. + } else {
  254. + (item).Set(v8key, env.Undefined());
  255. + }
  256. +}
  257. +
  258. void FlushBaton::Execute() {
  259. DWORD purge_all = PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR;
  260. if (!PurgeComm(int2handle(fd), purge_all)) {
  261. diff --git a/node_modules/@serialport/bindings-cpp/src/serialport_win.h b/node_modules/@serialport/bindings-cpp/src/serialport_win.h
  262. index f3e3c32..739b5dd 100644
  263. --- a/node_modules/@serialport/bindings-cpp/src/serialport_win.h
  264. +++ b/node_modules/@serialport/bindings-cpp/src/serialport_win.h
  265. @@ -47,23 +47,24 @@ Napi::Value Read(const Napi::CallbackInfo& info);
  266. Napi::Value List(const Napi::CallbackInfo& info);
  267. void setIfNotEmpty(Napi::Object item, std::string key, const char *value);
  268. +void setIfNotEmpty(Napi::Object item, std::string key, const wchar_t *value);
  269. struct ListResultItem {
  270. - std::string path;
  271. - std::string manufacturer;
  272. - std::string serialNumber;
  273. - std::string pnpId;
  274. - std::string locationId;
  275. - std::string friendlyName;
  276. - std::string vendorId;
  277. - std::string productId;
  278. + std::wstring path;
  279. + std::wstring manufacturer;
  280. + std::wstring serialNumber;
  281. + std::wstring pnpId;
  282. + std::wstring locationId;
  283. + std::wstring friendlyName;
  284. + std::wstring vendorId;
  285. + std::wstring productId;
  286. };
  287. struct ListBaton : public Napi::AsyncWorker {
  288. ListBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:ListBaton"),
  289. errorString() {}
  290. std::list<ListResultItem*> results;
  291. - char errorString[ERROR_STRING_SIZE];
  292. + wchar_t errorString[ERROR_STRING_SIZE];
  293. void Execute() override;
  294. void OnOK() override {