Skip to content

Commit d52f502

Browse files
committed
Windows: another attempt to support unicode argv
1 parent 9364699 commit d52f502

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
'FD_SETSIZE=1024',
161161
# we need to use node's preferred "win32" rather than gyp's preferred "win"
162162
'PLATFORM="win32"',
163+
'_UNICODE=1',
163164
],
164165
'libraries': [ '-lpsapi.lib' ]
165166
},{ # POSIX

src/node_main.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,47 @@
2121

2222
#include <node.h>
2323

24+
#ifdef _WIN32
25+
int wmain(int argc, wchar_t *wargv[]) {
26+
// Convert argv to to UTF8
27+
char** argv = new char*[argc];
28+
for (int i = 0; i < argc; i++) {
29+
// Compute the size of the required buffer
30+
DWORD size = WideCharToMultiByte(CP_UTF8,
31+
0,
32+
wargv[i],
33+
-1,
34+
NULL,
35+
0,
36+
NULL,
37+
NULL);
38+
if (size == 0) {
39+
// This should never happen.
40+
fprintf(stderr, "Could not convert arguments to utf8.");
41+
exit(1);
42+
}
43+
// Do the actual conversion
44+
argv[i] = new char[size];
45+
DWORD result = WideCharToMultiByte(CP_UTF8,
46+
0,
47+
wargv[i],
48+
-1,
49+
argv[i],
50+
size,
51+
NULL,
52+
NULL);
53+
if (result == 0) {
54+
// This should never happen.
55+
fprintf(stderr, "Could not convert arguments to utf8.");
56+
exit(1);
57+
}
58+
}
59+
// Now that conversion is done, we can finally start.
60+
return node::Start(argc, argv);
61+
}
62+
#else
63+
// UNIX
2464
int main(int argc, char *argv[]) {
2565
return node::Start(argc, argv);
2666
}
67+
#endif

0 commit comments

Comments
 (0)