Skip to content

Instantly share code, notes, and snippets.

@lazka

lazka/a.c Secret

Last active June 22, 2019 09:11
Show Gist options
  • Save lazka/3d828f9ed3bdf6b981eeb5edca65c657 to your computer and use it in GitHub Desktop.
Save lazka/3d828f9ed3bdf6b981eeb5edca65c657 to your computer and use it in GitHub Desktop.
cygwin arg passing
#include <Windows.h>
#include <stdio.h>
int main(void) {
LPCWSTR applicationName = L"C:\\cygwin64\\bin\\echo.exe";
LPWSTR commandLine = L"-- -FOO=\\\"BAR\\\"";
printf("# Using CommandLineToArgvW:\n");
LPWSTR *arg_list;
int arg_count;
WCHAR fullCommandLine[MAX_PATH] = L"";
wcscat(fullCommandLine, applicationName);
wcscat(fullCommandLine, L" ");
wcscat(fullCommandLine, commandLine);
arg_list = CommandLineToArgvW(fullCommandLine, &arg_count);
for(int i=0; i<arg_count; i++)
printf("%d: %ws\n", i, arg_list[i]);
printf("# Using CreateProcessW and cygwin echo.exe:\n");
BOOL ret;
PROCESS_INFORMATION pi;
STARTUPINFOW si;
si.cb = sizeof(si);
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
ret = CreateProcessW(
applicationName, commandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
return 0;
}
# Using CommandLineToArgvW:
0: C:\cygwin64\bin\echo.exe
1: --
2: -FOO="BAR"
# Using CreateProcessW and cygwin echo.exe:
-FOO=\BAR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment