CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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...



    • 'ShellExecute()'
      Code:
          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;
            }
          }



    • 'ShellExecuteEx()'
      Code:
          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()'



    • 'CreateProcess()'
      Code:
          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 as shown in the FAQ 'How can I wait until a process ends?'


    Last edited by Andreas Masur; July 21st, 2005 at 04:30 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured