Processes: How can I start a process?

Q: How can I start a process ?
A: There are several ways to start a process:

  • 'system()' family (C run-time library - ANSI ('system()') or Win NT ('_wsystem()'))
  • '_exec()' family (C run-time library - Win 95, Win NT)
  • '_spawn()' family (C run-time library - Win 95, Win NT)
  • 'WinExec()' (Win32 API)
  • 'ShellExecute()' (Shell API)
  • 'ShellExecuteEx()' (Shell API)
  • 'CreateProcess()' (Win32 API)
  • 'CreateProcessAsUser()' (Win32 API)
  • 'CreateProcessWithLogonW()' (Win32 API)

Most common are 'ShellExecute()', ShellExecuteEx()' and 'CreateProcess()'. Note that 'WinExec()' is provided only for compatibility with 16-bit Windows and should not be used any longer. The following examples will show how to display the text file 'c:\example.txt' in 'notepad.exe' using these three functions...

  1. 'ShellExecute()'
    HINSTANCE hInst = ShellExecute(0,
    "open", // Operation to perform
    "c:\\windows\\notepad.exe", // Application name
    "c:\\example.txt", // Additional parameters
    0, // Default directory
    SW_SHOW);
    if
    (reinterpret_cast<int>(hInst) <= 32)
    {
    // Could not start application
    switch(reinterpret_cast<int>(hInst))
    {
    case 0:
    // The operating system is out of memory or resources.
    break;
    case
    ERROR_FILE_NOT_FOUND:
    // The specified file was not found.
    break;
    case
    ERROR_PATH_NOT_FOUND:
    // The specified path was not found.
    break;
    case
    ERROR_BAD_FORMAT:
    // The .exe file is invalid (non-Microsoft Win32 .exe or error in .exe image).
    break;
    case
    SE_ERR_ACCESSDENIED:
    // The operating system denied access to the specified file.
    break;
    case
    SE_ERR_ASSOCINCOMPLETE:
    // The file name association is incomplete or invalid.
    break;
    case
    SE_ERR_DDEBUSY:
    // The Dynamic Data Exchange (DDE) transaction could not be completed because
    // other DDE transactions were being processed.
    break;
    case
    SE_ERR_DDEFAIL:
    // The DDE transaction failed.
    break;
    case
    SE_ERR_DDETIMEOUT:
    // The DDE transaction could not be completed because the request timed out.
    break;
    case
    SE_ERR_DLLNOTFOUND:
    // The specified dynamic-link library (DLL) was not found.

    case SE_ERR_FNF:
    // The specified file was not found.
    break;
    case
    SE_ERR_NOASSOC:
    // There is no application associated with the given file name extension.
    // This error will also be returned if you attempt to print a file that is
    // not printable.
    break;
    case
    SE_ERR_OOM:
    // There was not enough memory to complete the operation.
    break;
    case
    SE_ERR_PNF:
    // The specified path was not found.
    break;
    case
    SE_ERR_SHARE:
    // A sharing violation occurred.
    break;
    }
    }
  2. 'ShellExecuteEx()'
    SHELLEXECUTEINFO ExecuteInfo;
    memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
    ExecuteInfo.cbSize = sizeof(ExecuteInfo);
    ExecuteInfo.fMask = 0;
    ExecuteInfo.hwnd = 0;
    ExecuteInfo.lpVerb = "open"; // Operation to perform
    ExecuteInfo.lpFile = "c:\\windows\\notepad.exe"; // Application name
    ExecuteInfo.lpParameters = "c:\\example.txt"; // Additional parameters
    ExecuteInfo.lpDirectory = 0; // Default directory
    ExecuteInfo.nShow = SW_SHOW;
    ExecuteInfo.hInstApp = 0;
    if
    (ShellExecuteEx(&ExecuteInfo) == FALSE)
    // Could not start application -> call 'GetLastError()'
  3. 'CreateProcess()'
    STARTUPINFO siStartupInfo;
    PROCESS_INFORMATION piProcessInfo;
    memset(&siStartupInfo, 0, sizeof(siStartupInfo));
    memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    siStartupInfo.cb = sizeof(siStartupInfo);
    if
    (CreateProcess("c:\\windows\\notepad.exe", // Application name
    " example.txt", // Application arguments
    0,
    0,
    FALSE,
    CREATE_DEFAULT_ERROR_MODE,
    0,
    0, // Working directory
    &siStartupInfo,
    &piProcessInfo)
    == FALSE)
    // Could not start application -> call 'GetLastError()'
    In general 'ShellExecuteEx()' and even 'CreateProcess()' provides more controlling features of the application to start and its termination.

Note: Most of the material in this article is taken from codeguru.com


Recommended Reading :





Cheap Gas - Credit Card Consolidation - Mobile Phone - Internet Marketing