Discussion Locked

C++ CreateProcess

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Need working sample code for the Create Process function.

3 replies

by lastorck

void GetCheckValue(const string& fullFileName, unsigned* pValue, unsigned* pLen = 0)
{
const string FixFilePath = "fix.exe";
const DWORD TimeOutForFix = 10000;

string cmdLine = FixFilePath + " " + fullFileName;

HANDLE ReadPipe, WritePipe;
if( !CreatePipe(&ReadPipe, &WritePipe, 0, 0) )
throw string("Can't create pipe!\n");

STARTUPINFO StartupInfo;
memset(&StartupInfo, 0, sizeof(StartupInfo));
StartupInfo.cb = sizeof(StartupInfo);
StartupInfo.hStdOutput = WritePipe; StartupInfo.dwFlags = STARTF_USESTDHANDLES;
PROCESS_INFORMATION ProcessInformation;
if(!CreateProcess(0, const_cast(cmdLine.c_str()), 0, 0, TRUE,
NORMAL_PRIORITY_CLASS, 0, 0, &StartupInfo, &ProcessInformation ) )
{char str[150];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language ???
str, 149, NULL );
throw string("Can't start ") + FixFilePath+ string(" : ")
+ string(str) + string("\n");
}

if(!CloseHandle(WritePipe))
throw string("Can't close write pipe!\n");

char buff[255];
unsigned long count = 0;
if(!ReadFile(ReadPipe, buff, 250, &count, 0) )
throw string("Can't read from pipe!\n");

if(!CloseHandle(ReadPipe))
throw string("Can't close read pipe!\n");

if(WaitForSingleObject(ProcessInformation.hProcess, TimeOutForFix) != WAIT_OBJECT_0)
throw string("Can't execute ") + FixFilePath + string("\n");

}

by hueristic

Sorry..Probally should have ellabrated...I'm a novice and I'm porting a program I wrote as a console app to a win32 app and need a simple funtional program to call a program passing command line parameters...need to also control the thred because after the called program is finished need to do some cleaning up.
This function is a nice peice of work but it would take me more time to figure out than it's worth at this point at my skillset level. BTW Looked up throw and it's the 3rd part of a3 part exception handler?

This discussion is closed to new replies.