Skip to content

Instantly share code, notes, and snippets.

@SpaceManiac
Created September 17, 2017 07:57
Show Gist options
  • Save SpaceManiac/3c1ee2e2088107838718254545a08304 to your computer and use it in GitHub Desktop.
Save SpaceManiac/3c1ee2e2088107838718254545a08304 to your computer and use it in GitHub Desktop.
CommandLineToArgvW differs from argc/argv
D:\projects\Sandbox>"cpp/"a.exe "cpp/"a.exe
---- argc and argv think:
0: cpp/a.exe
1: cpp/a.exe
---- GetCommandLineW is:
"cpp/"a.exe "cpp/"a.exe
---- CommandLineToArgvW thinks:
0: cpp/
1: a.exe
2: cpp/a.exe
#include <stdio.h>
#include <windows.h>
int main(int argc, char* argv[]) {
printf("---- argc and argv think:\n");
for (int i = 0; i < argc; ++i) {
printf("%i: %s\n", i, argv[i]);
}
printf("---- GetCommandLineW is:\n");
printf("%ws\n", GetCommandLineW());
printf("---- CommandLineToArgvW thinks:\n");
LPWSTR *szArglist;
int nArgs;
szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
if (szArglist == NULL) {
wprintf(L"CommandLineToArgvW failed\n");
return 0;
}
for (int i = 0; i < nArgs; i++) {
printf("%d: %ws\n", i, szArglist[i]);
}
LocalFree(szArglist);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment