CreateProcess not working as doc specifies.
- From: "David Bennion" <David Bennion@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 13 Jun 2005 13:50:17 -0700
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
.
- Follow-Ups:
- Re: CreateProcess not working as doc specifies.
- From: Walter Briscoe
- Re: CreateProcess not working as doc specifies.
- Prev by Date: Re: api functions for...
- Next by Date: Re: What would happen if a driver leaks?
- Previous by thread: api functions for...
- Next by thread: Re: CreateProcess not working as doc specifies.
- Index(es):
Relevant Pages
|