Programmatically Launching Windows NT Applications

Wednesday Jan 24th 2001 by Michel Yossef David
Share:

Two utilities for launching NT application synchronously or asynchronously

In our version of RunProcessAndWait, we don't wait more that 300s (5mn) if the process is still working or stucked, the function exits with a FALSE return value.

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 created
FALSE 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;	
}

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved