CreateProcess not working as doc specifies.



I am having some difficulty using CreateProcess() to run a batch file and get
the same return code as I get from the command shell. I am trying to run the
batch in the manner suggested by the CreateProcess documentation, but it
fails to run.

I am posting all of the background information below (sorry if the wordwrap
from the post obfuscates anything):

Snippet of doc from CreateProcess() msdn doc:
--------------------------------------------------------
"To run a batch file, you must start the command interpreter; set
lpApplicationName to cmd.exe and set lpCommandLine to the name of the batch
file."

cp.c example app calls CreateProcess() to run a .bat file
----------------------------------------------------------------
#include <windows.h>

int main(int argc,char ** argv)
{

PROCESS_INFORMATION ProcessInfo; //This is what we get as an [out] parameter
STARTUPINFO StartupInfo; //This is an [in] parameter
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo ; //Only compulsory field

// four different ways of calling CreateProcess() shown below

// according to the doc this should work, but it doesn't.
if(CreateProcess("cmd.exe", "C:\\temp\\example\\test.bat",
// but this one doesn't actully run it
//if(CreateProcess("C:\\winnt\\system32\\cmd.exe",
"C:\\temp\\example\\test.bat",
//if(CreateProcess("C:\\winnt\\system32\\cmd.exe", "/C
C:\\temp\\example\\test.bat",
// the above behaves exactly as this:
//if(CreateProcess(NULL,"C:\\winnt\\system32\\cmd.exe /C
C:\\temp\\example\\test.bat",
NULL,NULL,FALSE,0,NULL,
NULL,&StartupInfo,&ProcessInfo))
{
DWORD ExitCode;
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,&ExitCode);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
printf("The process returned exit code %d\n",ExitCode);

}
else
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0,NULL);
printf("Error creating job object: %s\n",(char*)lpMsgBuf);
LocalFree(lpMsgBuf);
}

}

the test.bat (should return exit code 1 and does when run from the command
line -- validated via subsequent echo %errorlevel%):
-----------------------------------------------------------------------------------------
c:\temp\example\dummyapp.exe 1
if "%ERRORLEVEL%"=="0" GOTO :NEXTSTEP
GOTO DONE

:NEXTSTEP
c:\temp\example\dummyapp.exe 0
echo %errorlevel%

:DONE
echo %errorlevel%

simple source to dummyapp.exe test -- return as exit code whatever
the command line specified
-------------------------------------------------------------------
#include <string.h>
int main(int argc,char ** argv)
{
int x;
if(argc > 0)
x = strtoul(argv[1],NULL,10);
return x;
}

Output from the doc method (using "cmd.exe" in lpApplication)
-------------------------------------------------------------------
The process could not be started...
Error creating job object: The system cannot find the file specified.

Output from the second method (never really runs the command)
-------------------------------------------------------------------
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

Output from the third and fourth methods (using cmd.exe /c to run the batch
file)
-------------------------------------------------------------------
The process returned exit code 0
.



Relevant Pages

  • Re: Create Process Help
    ... > I've had good luck with the following, where Command is the name of the ... > GetExitCodeProcess(pi.hProcess, &rtn); ... > // CREATEPROCESS FAILED error ... was a result of the line in my batch code. ...
    (microsoft.public.vc.language)
  • Re: CreateProcess not working as doc specifies.
    ... >>I am having some difficulty using CreateProcess() to run a batch file and get ... >>the same return code as I get from the command shell. ... I can successfully get the batch file to run, ... it returns exit code 0. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: CreateProcess not working as doc specifies.
    ... >>>the same return code as I get from the command shell. ... >>>batch in the manner suggested by the CreateProcess documentation, ... I can successfully get the batch file to run, ... it returns exit code 0. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Multi-zip files -- Windows server
    ... native DOS versions). ... TO BATCH OR NOT TO BATCH ... and using a batch file to try and solve the problem... ... The IBM PC operating system command line is a most ...
    (comp.sys.cbm)
  • Re: Booming or Fuming? Are You Alive? Or Are You Fading Away?!
    ... purely of statements processed completely by the native batch command ... > command processor, only because it comes free with the OS. ... Hey, guard, it is not PERFECT, OK? ...
    (microsoft.public.win2000.cmdprompt.admin)