Introduction
There a quite a few ways to start a process
- C run-time libraries
- system()
- _exec()
- _spawn()
- Win32 API
- WinExec() - Provided only for compatibility with 16-bit Windows and should not be used any longer.
- CreateProcess()
- CreateProcessAsUser()
- CreateProcessWithLogonW()
- 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");
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 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);
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);

