Programmatically Launching Windows NT Applications | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 24, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.