I would like the Win32 process to run an external script and be able to get the returned ERRORLEVEL.
The other way is pretty simple. Just use exit (errorcode) or the return error code in your Win32 application, and the calling script will correctly set the value to ERRORLEVEL.
But from the calling Win32 application, getting the script error code is not exactly the same.
I tried using CreateProcess () and called GetExitCodeProcess (), but it always returns 0, not the actual value of ERRORLEVEL. Even if I ended my script call with exit% ERRORLEVEL%
My best guess is that the script is not a process. Most likely, CMD.EXE starts instead and most likely always ends with ExitCode 0. I know that ERRORLEVEL does not match the value of the ExitCode parameter, I would hope that CMD.EXE would reflect it.
EDIT:
Sorry, I asked! I just found a MY problem . I used exit / b errorcode instead of the exit error code in my batch file. It seems that the / b options take precedence only when closing a script run, rather than CMD.EXE when you are testing from the command line. But the disadvantage is the lack of the correct ExitCode for CMD.EXE.
So, for posterity, I do:
int LaunchScript(TCHAR *pCmdLineParam)
{
bool bResult;
PROCESS_INFORMATION pi;
STARTUPINFO si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
TCHAR cmdLine[256];
_tcscpy(cmdLine,L"Test.cmd");
_tcscat(cmdLine,L" ");
_tcscat(cmdLine,pCmdLineParam);
_tprintf(L"Process executing: %s\n",cmdLine);
bResult = CreateProcess(NULL, cmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)?true:false;
if (!bResult) {
_tprintf(L"CreateProcess() error - %d", GetLastError());
return -1;
}
DWORD result = WaitForSingleObject(pi.hProcess,15000);
if( result == WAIT_TIMEOUT ) {
return -2;
}
DWORD exitCode=0;
if( !GetExitCodeProcess(pi.hProcess,&exitCode) ) {
_tprintf(L"GetExitCodeProcess() error - %d", GetLastError());
return -1;
}
_tprintf(L"Process exitcode=%d\n",exitCode);
return exitCode;
}
My batch file "entry point" is as follows:
@call %*
@exit %ERRORLEVEL%
script script. "-" exit/b exit, .