Click to See Complete Forum and Search --> : CreateProcess()


jAyesh
May 9th, 1999, 10:13 AM
Hi all,

Does anyone have a sample program that shows how to use the CreateProcess() and TerminateProcess() functions.
In the OLD MSDN Libraries - 97, there was a sample program called PROCESS that talked about this, but I cant seem to find that in the online MSDN site. Does anyoen have this sample.

Would appreciate if anyone could send it to my e-mail address. Thanks alot!

Any help will be highly appreciated.

Cheers
jAyesh

I searched online MSDN from the web, but couldnt find. Could you please
send me the sample program, as am sure you guys have the old MSDN
libraries.

I know its a hassle for you. But I would highly appreciate this.

Thanks
jAyesh

Danielle Harvey
May 9th, 1999, 08:05 PM
Here is the code to run another program using CreateProcess().

Void RunAnother(LPSTR lpszFilename)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si); // start child process

if (!CreateProcess(NULL,
lpszFilename,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi))
{
Error("CreateProcess Failed");
return;
}

// wait until child process exits
WaitForSingleObject(ph.iProcess, INFINITE);

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

So, to run notepad.exe, you type somewhere in your code,

const char cmdLine[] = "\\windows\\notepad.exe";
RunAnother(cmdLine);

I still use WinExec() to run other programs, in which case the code
to run notepad.exe is;

const char cmdLine[] = "\\windows\\notepad.exe";
UINT result = WinExec(cmdLine, SW_SHOW);
if (result < 32)
{
// handle error
}

I have never tried using TerminateProcess(). Hope this helps.
Email me if you have any questions about CreateProcess().

pabitra
May 10th, 1999, 06:05 AM
Hi,

Here is the sample programme for create process.

Thanks,
Pabitra


void CMainFrame::OnToolsConsole()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;

CString strApp = _T("CONSOLE.EXE");

// Initialize the STARTUPINFO structure.
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
CreateProcess(
NULL, // pointer to name of executable module
(char *)(LPCTSTR) strApp, // pointer to command line string
NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes
FALSE, // handle inheritance flag
0, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&si, // pointer to STARTUPINFO
&pi // pointer to PROCESS_INFORMATION
);
}

William Walseth
May 10th, 1999, 10:26 AM
Just a quick heads up, the CreateProcess() is now obsolete, and is not compliant with the Windows '95 & NT logo programs.

For the details, see the MSJ article at http://www.microsoft.com/MSJ/0299/ednote0299top.htm
and the real logo page at http://msdn.microsoft.com/winlogo/win2000.asp

Instead use ShellExecute()

Here’s a quick example:

if( ( hRc = ShellExecute( AfxGetMainWnd()->GetSafeHwnd(),
"open",
szProgramName,
szCommandLine,
szWorkingDir,
SW_SHOW ) ) <= (HINSTANCE)32 )
{
// Message that it failed.
}