Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/articles/win32-tutorials/)
-   -   Start a process (http://www.go4expert.com/articles/start-a-process-t17572/)

coderzone 17May2009 13:26

Start a process
 

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 [inlinecode]LogonUser[inlinecode] 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.

shabbir 3Jun2009 09:39

Re: Start a process
 
Nomination this Article for Article of the month - May 2009

rajeev_rsd 16Sep2009 06:41

Re: Start a process
 
"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.


All times are GMT +5.5. The time now is 08:57.