TL;DR:
CreateProcess(?, ?, ?, ...)
for:
- Pass current process params (i.e. "batchfile"
%*
) - correctly connect stdin and stdout
- creation flags?
I have the following problem:
- I need to launch a given 3rd party executable with a custom environment and custom parameters. (Both semi-fixed)
- I cannot use a batch file, because the (again, 3rd party) side invoking the module directly calls
CreateProcess
- I need to pass on any additional paramers passed
So, what I'd like to do is create a very simple executable launcher that would be the equivalent of a batch file like:
set PATH=...
set WHATEVER=...
...\3rd-pty-tool.exe -switch1 -switch2 %*
exit /B %ERRORLEVEL%
And I certainly don't want to mess with any bat2exe converter stuff - just too ugly when I have Visual Studio around anyway.
Running another executable via CreateProcess
is trivial in principle:
STARTUPINFO info={sizeof(info)};
PROCESS_INFORMATION processInfo;
if (CreateProcess(?, ?, ?, ?, ?, ?, ?, ?, &info, &processInfo))
{
WaitForSingleObject(processInfo.hProcess, INFINITE);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
Setting up the environment for the child process via _putenv
et al. is also pretty easy.
What is not trivial to me is however what to pass on to CreateProcess
:
BOOL WINAPI CreateProcess(
_In_opt_ LPCTSTR lpApplicationName,
_Inout_opt_ LPTSTR lpCommandLine,
_In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes,
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ BOOL bInheritHandles,
_In_ DWORD dwCreationFlags,
_In_opt_ LPVOID lpEnvironment,
_In_opt_ LPCTSTR lpCurrentDirectory,
_In_ LPSTARTUPINFO lpStartupInfo,
_Out_ LPPROCESS_INFORMATION lpProcessInformation
);
- How to I get at the
%*
equivalent for the current Win32 process? - Pass only
lpApplicationName
, onlylpCommandLine
or both? - What to do about handle inheritance and creation flags?
- How to I correctly forward / return stdin and stdout?
Not a dupe: CreateProcess to execute Windows command