Tuesday, 3 December 2013

Difference between CreateProcess and ShellExecute API Calls

Difference between CreateProcess and ShellExecute API Calls

CreateProcess and ShellExecute API Calls are very useful and closely related. In this article, I have tried to make clear the difference between CreateProcess and ShellExecute API Calls. You should know where and why to use CreateProcess and ShellExecute API Calls in your application. So here are some points of difference between CreateProcess and ShellExecute API Calls.

1. CreateProcess() is more oriented on low level and ShellExecute() on the high user level:

For example using of CreateProcess with correct parameters you can start the program which has the path larger as MAX_PATH. It has 32,768 characters restriction. You can also use CreateProcess to start program (if you have enough permissions) on another windows desktop like on the Logon Screen.

Another example: You can use ShellExecute to start Control Panel or open any program which existed on the computer for editing of JPG file for example. So you work with ShellExecute close to the corresponding actions in the Windows Explorer.

2. ShellExecute() is more flexible than CreateProcess(): An extension of my first point

ShellExecute() is a shell around the OS and defaults to the OS to run programs, which gives it a lot more flexibility than CreateProcess(), which engages the program directly, which is why you can't run, say, HTML files, or PDF files, etc., with CreateProcess(), but which can be run with ShellExecute().

ShellExecute() is a wrapper to the process of:

1. Determining the type of file by searching the Windows Registry.
2. Enumerating the allowed shell commands (verbs).
3. Retrieving the command line for the specified verb.
4. Constructing the command-line switches.
5. Calling CreateProcess() to start the process pointed to by the retrieved command line.

3. Handle Information

CreateProcess returns the handle and id for the started process and it's main thread in the PROCESS_INFORMATION structure. By using these handles, you can shut down the process whenever required but no handle is returned by ShellExecute().

Suggestion: If you just want to launch a file in a browser or notepad etc., ShellExecute() is probably what you want. However, if you know the exact .exe name you want to launch, using CreateProcess() gives you much more control.

Note: Do not use WinExec() as it is a deprecated function from the 16-bit days.

No comments:

Post a Comment