Programmatically Launching Windows NT Applications

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



Comments

  • Thank you vvvvvvvvvvvery much!!~

    Posted by Legacy on 03/14/2003 12:00am

    Originally posted by: Jin Ki Byung

    lol

    Reply
  • access violation problem

    Posted by Legacy on 06/12/2002 12:00am

    Originally posted by: WolV

    I tried your code with my app..the second app was called during debugging but there is also an access violation..I've tried playing around with the code but nothings improved..
    
    program written in vc++6.0 enterprise
    os: win98se
    mfc app .exe
    simple dialog ui to call a second program
    error msg: First-chance exception in try.exe: 0xC0000005: Access Violation.

    any ideas for this dumb fool?..anyone?..

    Reply
  • Environment Inheritance

    Posted by Legacy on 04/17/2002 12:00am

    Originally posted by: Rob Moore

    Would you expect a _putenv("TMPDIR=c:/temp") before the
    CreateProcess 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.

    Reply
  • WORD-EXCEL

    Posted by Legacy on 02/28/2002 12:00am

    Originally posted by: dogu

    If you run more than one word or excel application, WaitForSingleObject returns immediately. Do you have any solution for this situation ?

    Reply
  • How to use Applications programmatically

    Posted by Legacy on 01/30/2002 12:00am

    Originally 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

    Reply
  • Programmatically launching Windows NT application Remotely.

    Posted by Legacy on 11/21/2001 12:00am

    Originally posted by: Munnu

    Please let me know, how can I launch Windows application remotely. It should not run as background process. It should run in foreground as we start application by double clicking. For example if I start notepad.exe, its window should appear on the remote machine.
    
    

    I appreciate your help. Please help me in find solution for it.

    Thanks

    Regards
    S. Munvar

    Reply
  • Can we extend to catch the output of child process?

    Posted by Legacy on 04/18/2001 12:00am

    Originally 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,
    Anand.

    Reply
  • Invoking a process thru other program

    Posted by Legacy on 04/16/2001 12:00am

    Originally posted by: Mahesh Lotake

    Is there a way where I can find a perticular process already running and instead of creating/invoking new process i can just activate the already running process.
    
    For example if PKZIP is already running then no need to spawn another instance but activate the current instance by bringing it on the top.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Information is data with context. The era of Big Data has begun demonstrating to information security that there is more that can, and must, be done to identify threats, reduce risk, address fraud, and improve compliance monitoring activities by bringing better context to data and thereby creating information for actionable intelligence. This analyst report sets the stage and provides insights into IT and information security practitioners' perceptions of the impediments to, and the solutions necessary for, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds