Programmatically Launching Windows NT Applications
Posted
by Michel Yossef David
on January 24th, 2001
RunAndForgetProcess(..) just run a create the command line process and exit.
Note:
Using DDE in the child process of 'RunProcessAndWait' may give some deadlock. (Thank for the comments !)Enough explanations for a so few code lines!
Return Value:
TRUE if the process was createdFALSE if it not.
See *nRetValue for LastError
Code
#include "windows.h"
#include <process.h>
BOOL RunProcessAndForget(CString sCmdLine,
CString sRunningDir,
int *nRetValue);
BOOL RunProcessAndWait(CString sCmdLine,
CString sRunningDir,
int *nRetValue);
//---------------------------------------------------------
// Run a synchronized other command line EXE. Returns only
// after this exits. The process is runned as a console window.
// Returns Values : TRUE if the process was created
// FALSE if not.
// see *nRetValue for the LastError number
BOOL RunProcessAndWait(CString sCmdLine,
CString sRunningDir,
int *nRetValue)
{
int nRetWait;
int nError;
// That means wait 300 s before returning an error
// You can change it to the value you need.
// If you want to wait for ever just use 'dwTimeout = INFINITE'>
DWORD dwTimeout = 1000 *300;
STARTUPINFO stInfo;
PROCESS_INFORMATION prInfo;
BOOL bResult;
ZeroMemory( &stInfo, sizeof(stInfo) );
stInfo.cb = sizeof(stInfo);
stInfo.dwFlags=STARTF_USESHOWWINDOW;
stInfo.wShowWindow=SW_MINIMIZE;
bResult = CreateProcess(NULL,
(LPSTR)(LPCSTR)sCmdLine,
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE
| NORMAL_PRIORITY_CLASS,
NULL,
(LPCSTR)sRunningDir,
&stInfo,
&prInfo);
*nRetValue = nError = GetLastError();
if (!bResult) return FALSE;
nRetWait = WaitForSingleObject(prInfo.hProcess,dwTimeout);
CloseHandle(prInfo.hThread);
CloseHandle(prInfo.hProcess);
if (nRetWait == WAIT_TIMEOUT) return FALSE;
return TRUE;
}
//---------------------------------------------------------
// This function call a command line process.
// Returns Values : TRUE if the process was created
// FALSE if not.
// see *nRetValue for the LastError number
BOOL RunAndForgetProcess(CString sCmdLine,
CString sRunningDir,
int *nRetValue)
{
int nRetWait;
int nError;
STARTUPINFO stInfo;
PROCESS_INFORMATION prInfo;
BOOL bResult;
ZeroMemory( &stInfo, sizeof(stInfo) );
stInfo.cb = sizeof(stInfo);
stInfo.dwFlags=STARTF_USESHOWWINDOW;
stInfo.wShowWindow=SW_MINIMIZE;
bResult = CreateProcess(NULL,
(LPSTR)(LPCSTR)sCmdLine,
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE
| NORMAL_PRIORITY_CLASS ,
NULL,
(LPCSTR)sRunningDir ,
&stInfo,
&prInfo);
*nRetValue = nError = GetLastError();
// Don't write these two lines if you need
CloseHandle(prInfo.hThread);
// to use these handles
CloseHandle(prInfo.hProcess);
if (!bResult) return FALSE;
return TRUE;
}

Comments
Thank you vvvvvvvvvvvery much!!~
Posted by Legacy on 03/14/2003 12:00amOriginally posted by: Jin Ki Byung
lol
Replyaccess violation problem
Posted by Legacy on 06/12/2002 12:00amOriginally posted by: WolV
ReplyEnvironment Inheritance
Posted by Legacy on 04/17/2002 12:00amOriginally posted by: Rob Moore
Would you expect a _putenv("TMPDIR=c:/temp") before the
ReplyCreateProcess to be lost in the tool you just kicked off ?
I can get this to work ok under NT but under W2000 the
sub-process does not seem to inherit the TMPDIR variable.
WORD-EXCEL
Posted by Legacy on 02/28/2002 12:00amOriginally posted by: dogu
If you run more than one word or excel application, WaitForSingleObject returns immediately. Do you have any solution for this situation ?
ReplyHow to use Applications programmatically
Posted by Legacy on 01/30/2002 12:00amOriginally posted by: Tamer
Hi ,
I want to run for ex. Notepade.EXE as a new process , then I want programmatically to paste what in the clipboard to this application , send to it WM_PASTE.
Tamer
ReplyProgrammatically launching Windows NT application Remotely.
Posted by Legacy on 11/21/2001 12:00amOriginally posted by: Munnu
ReplyCan we extend to catch the output of child process?
Posted by Legacy on 04/18/2001 12:00amOriginally posted by: G E Anand
Hi,
Does anyone know how to create a child process and to catch the output which it generates?
Ex. Say I create the new process as Command.com and would like to display all the output which it gives in my process, then how is this achieved?
Regards,
ReplyAnand.
Invoking a process thru other program
Posted by Legacy on 04/16/2001 12:00amOriginally posted by: Mahesh Lotake
Reply