1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Start a process

Discussion in 'Win32' started by coderzone, May 17, 2009.

  1. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0

    Introduction



    There a quite a few ways to start a process
    1. C run-time libraries
      • system()
      • _exec()
      • _spawn()
    2. Win32 API
      • WinExec() - Provided only for compatibility with 16-bit Windows and should not be used any longer.
      • CreateProcess()
      • CreateProcessAsUser()
      • CreateProcessWithLogonW()
    3. Shell API
      • ShellExecute()
      • ShellExecuteEx()

    C run-time libraries



    system

    The function system() takes a string which will be parsed by the shell.
    Code:
    system("notepad");
    _exec

    When a call to an _exec function is successful, the new process is placed in the memory previously occupied by the calling process. Sufficient memory must be available for loading and executing the new process.

    Refer MSDN for complete description and sample.

    _spawn

    The _spawn functions each create and execute a new process.

    Refer MSDN for complete description and sample.

    Win32 API



    WinExec

    Provided only for compatibility with 16-bit Windows and should not be used any longer.

    CreateProcess

    Creates a new process which runs in the security context of the calling process.

    Code:
    STARTUPINFO	si;
    PROCESS_INFORMATION pi;
    
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    
    // Start the child process. 
    BOOL bRetVal = ::CreateProcess( "notepad.exe",			  
    	NULL,			  // Command line. 
    	NULL,             // Process handle not inheritable. 
    	NULL,             // Thread handle not inheritable. 
    	FALSE,            // Set handle inheritance to FALSE. 
    	0,                // No creation flags. 
    	NULL,             // Use parent's environment block. 
    	NULL,             // Use parent's starting directory. 
    	&si,              // Pointer to STARTUPINFO structure.
    	&pi               // Pointer to PROCESS_INFORMATION structure.
    );
    
    CreateProcessAsUser

    CreateProcessAsUser creates the new process for the user. Its pretty myuch similar to the CreateProcess with an additional hToken which is handle to the primary token that represents a user. To get a primary token that represents the specified user, call the LogonUser function.

    CreateProcessWithLogonW

    This function is similar to the CreateProcessAsUser functions, except that the caller does not need to call the LogonUser function to authenticate the user and get a token but the function itself takes the username and password for the domain and can authenticate itself.

    Shell API



    ShellExecute

    Code:
    HINSTANCE result = ShellExecute(NULL, "open", "c:\\windows\\notepad.exe", NULL,NULL, SW_SHOW);
    ShellExecuteEx

    Code:
    SHELLEXECUTEINFO ExecuteInfo;
    
    memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
    
    ExecuteInfo.cbSize       = sizeof(ExecuteInfo);
    ExecuteInfo.fMask        = 0;                
    ExecuteInfo.hwnd         = 0;                
    ExecuteInfo.lpVerb       = "open";
    ExecuteInfo.lpFile       = "notepad.exe"
    ExecuteInfo.lpParameters = NULL;
    ExecuteInfo.lpDirectory  = 0; 
    ExecuteInfo.nShow        = SW_SHOW;
    ExecuteInfo.hInstApp     = 0;
    
    ShellExecuteEx(&ExecuteInfo);
    
    Most common are 'ShellExecute()', ShellExecuteEx()' and 'CreateProcess()'. 'ShellExecuteEx()' and even 'CreateProcess()' provides more controlling features of the application to start and its termination.
     
    Last edited: May 17, 2009
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
  3. rajeev_rsd

    rajeev_rsd New Member

    Joined:
    Sep 8, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    "CreateProcess

    Creates a new process which runs in the security context of the calling process.

    Code:
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    BOOL bRetVal = ::CreateProcess( "notepad.exe",
    NULL, // Command line.
    NULL, // Process handle not inheritable.
    NULL, // Thread handle not inheritable.
    FALSE, // Set handle inheritance to FALSE.
    0, // No creation flags.
    NULL, // Use parent's environment block.
    NULL, // Use parent's starting directory.
    &si, // Pointer to STARTUPINFO structure.
    &pi // Pointer to PROCESS_INFORMATION structure.
    );"

    Thanks For this, I got My program working and launching an application.

    Now can you explain a Method, Which can Launch a Process with Parameters (Sorry Complex Parameters) like for example "taskkill /F /IM vmnat.exe /T" as you can see both side parameters, can you please explain with a snippet of code, how we can use such parameters with the process, we are launching.

    Thanx in advance.
     

Share This Page