client/windows: fix cmdline parsing on W7

This commit is contained in:
Oleksii Shevchuk 2019-08-21 14:37:48 +03:00
parent f848cae5bf
commit c9cbba260b
1 changed files with 13 additions and 9 deletions

View File

@ -49,12 +49,11 @@ void redirect_stdout() {
// https://stackoverflow.com/questions/291424/ // https://stackoverflow.com/questions/291424/
LPSTR* CommandLineToArgvA(INT *pNumArgs) LPSTR* CommandLineToArgvA(INT *pNumArgs)
{ {
LPWSTR cmdline;
LPWSTR* args; LPWSTR* args;
LPSTR* result; LPSTR* result;
LPSTR buffer; LPSTR buffer;
BOOL lpUsedDefaultChar;
int retval; int retval;
int numArgs; int numArgs;
int storage; int storage;
@ -78,7 +77,14 @@ LPSTR* CommandLineToArgvA(INT *pNumArgs)
numArgs = 0; numArgs = 0;
args = CommandLineToArgvW_(GetCommandLineW(), &numArgs); cmdline = GetCommandLineW();
if (!cmdline) {
dprint("Command line not found");
*pNumArgs = 0;
return NULL;
}
args = CommandLineToArgvW_(cmdline, &numArgs);
if (args == NULL) { if (args == NULL) {
*pNumArgs = 0; *pNumArgs = 0;
return NULL; return NULL;
@ -87,10 +93,9 @@ LPSTR* CommandLineToArgvA(INT *pNumArgs)
storage = numArgs * sizeof(LPSTR); storage = numArgs * sizeof(LPSTR);
for (i = 0; i < numArgs; ++ i) for (i = 0; i < numArgs; ++ i)
{ {
lpUsedDefaultChar = FALSE;
retval = WideCharToMultiByte( retval = WideCharToMultiByte(
CP_UTF8, 0, args[i], -1, NULL, CP_UTF8, 0, args[i], -1, NULL,
0, NULL, &lpUsedDefaultChar 0, NULL, NULL
); );
if (!SUCCEEDED(retval)) if (!SUCCEEDED(retval))
{ {
@ -114,23 +119,22 @@ LPSTR* CommandLineToArgvA(INT *pNumArgs)
buffer = ((LPSTR)result) + (numArgs * sizeof(LPSTR)); buffer = ((LPSTR)result) + (numArgs * sizeof(LPSTR));
for (i = 0; i < numArgs; ++ i) for (i = 0; i < numArgs; ++ i)
{ {
BOOL lpUsedDefaultChar = FALSE;
if (bufLen < 0) { if (bufLen < 0) {
dprint("Buflen exhaused, arg %d (%d/%d)\n", i, bufLen, storage); dprint("Buflen exhaused, arg %d (%d/%d)\n", i, bufLen, storage);
numArgs = i;
break; break;
} }
retval = WideCharToMultiByte( retval = WideCharToMultiByte(
CP_UTF8, 0, args[i], -1, buffer, CP_UTF8, 0, args[i], -1, buffer,
bufLen, NULL, &lpUsedDefaultChar bufLen, NULL, NULL
); );
if (!SUCCEEDED(retval)) if (!SUCCEEDED(retval))
{ {
LocalFree(result); LocalFree(result);
LocalFree(args); LocalFree(args);
*pNumArgs = 0; *pNumArgs = i;
return NULL; return NULL;
} }