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/
LPSTR* CommandLineToArgvA(INT *pNumArgs)
{
LPWSTR cmdline;
LPWSTR* args;
LPSTR* result;
LPSTR buffer;
BOOL lpUsedDefaultChar;
int retval;
int numArgs;
int storage;
@ -78,7 +77,14 @@ LPSTR* CommandLineToArgvA(INT *pNumArgs)
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) {
*pNumArgs = 0;
return NULL;
@ -87,10 +93,9 @@ LPSTR* CommandLineToArgvA(INT *pNumArgs)
storage = numArgs * sizeof(LPSTR);
for (i = 0; i < numArgs; ++ i)
{
lpUsedDefaultChar = FALSE;
retval = WideCharToMultiByte(
CP_UTF8, 0, args[i], -1, NULL,
0, NULL, &lpUsedDefaultChar
0, NULL, NULL
);
if (!SUCCEEDED(retval))
{
@ -114,23 +119,22 @@ LPSTR* CommandLineToArgvA(INT *pNumArgs)
buffer = ((LPSTR)result) + (numArgs * sizeof(LPSTR));
for (i = 0; i < numArgs; ++ i)
{
BOOL lpUsedDefaultChar = FALSE;
if (bufLen < 0) {
dprint("Buflen exhaused, arg %d (%d/%d)\n", i, bufLen, storage);
numArgs = i;
break;
}
retval = WideCharToMultiByte(
CP_UTF8, 0, args[i], -1, buffer,
bufLen, NULL, &lpUsedDefaultChar
bufLen, NULL, NULL
);
if (!SUCCEEDED(retval))
{
LocalFree(result);
LocalFree(args);
*pNumArgs = 0;
*pNumArgs = i;
return NULL;
}