What are the main differences between the two? I'm willing to run only another EXE from my (C++) application. Are there any differences when inheriting environments, security features etc?
-
1The [MSDN docs](http://msdn.microsoft.com) give you this information. This question is entirely too vague and non-specific for SO. There are dozens of questions that discuss both, and you've shown absolutely no effort to research this yourself. (As a hint: Only one allows you to specify anything about the environment, inheriting handles, and other information. I'll leave it to your research abilities to figure out which one.) – Ken White May 25 '12 at 01:49
3 Answers
The main difference is in flexibility. ShellExecute
is easier to use, but doesn't have a lot of flexibility. CreateProcess
is a pain to use, but lets you do anything.
Just for example, with CreateProcess
, you can specify handles (pipes or files) to use for the standard input/output/error streams in the child. ShellExecute
doesn't give you want way to do that.
It's probably also worth noting that although ShellExecute
can be used to start an executable directly, its primary intent is to "execute" document files -- for example, tell it to "execute" a "whatever.html", and it starts up your default web browser and loads the specified HTML file into it. You can do that using CreateProcess
as well, but to do it, you (normally) start by calling FindExecutable
to find the program associated with the data file in question, then execute that passing your data file as a parameter.

- 476,176
- 80
- 629
- 1,111
The main difference between CreateProcess
and ShellExecute
is the following: CreateProcess
is more oriented on low level and ShellExec
on the high user lever which see the user in explorer.
For example using of CreateProcess
one can use command line which length is more 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 filed for example. So you works with ShellExecute
close to the corresponding actions in the Windows Explorer.

- 220,925
- 34
- 403
- 798
-
CreateProcess can NOT start a process if the path to the program is over MAX_PATH. It can pass long arguments to a program that can be started, but that's different. Basically there's no way at present to start a program that is situated deeper than MAX_PATH (unless there is a short 8.3 equivalent) – nikos Dec 29 '16 at 16:07
-
@nikos: One can use both `lpApplicationName` and `lpCommandLine` to specify the program with parameters. The maximal length of `lpCommandLine` is limited to 32,768 characters. You can read in [the documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx) that "If lpApplicationName is NULL, the module name portion of lpCommandLine is limited to MAX_PATH characters.". By using both non NULL `lpApplicationName` and `lpCommandLine` one should be able to specify the module part which length is larger as MAX_PATH. I think one should use "\\?\" prefix in the path – Oleg Dec 29 '16 at 16:23
-
@nikos: Moreover, starting in Windows 10, version 1607, MAX_PATH limitations have been removed from common Win32 file and directory functions (see [the initial announcement](https://mspoweruser.com/ntfs-260-character-windows-10/) and [the documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath)). – Oleg Dec 29 '16 at 16:42
-
@nikos: In many practical cases one can use more simple workaround to start reduce the long path to another more short one. One can use [GetShortPathName](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364989(v=vs.85).aspx) with the input path which starts with `"\\?\"` and to get more short name, which you can use instead. It's not the same, what I wrote before, but it's helpful to reduce more short paths of files. – Oleg Dec 29 '16 at 16:52
-
just try and start a truly deep program then tell me if you succeed ;) The theory is far from the practice. See here for more http://zabkat.com/blog/long-8.3-path-names.htm – nikos Dec 31 '16 at 08:37
-
@nikos: I'm not full understand your goal or your question. I can't make tests on old operation systems now. I changed the text of the old answer to specify only the length of `lpCommandLine` to reduce the unneeded discussion. If you need to create the process with the program with the long path, then I'd recommend you to use `RtlCreateUserProcess` with `RtlCreateProcessParameters`. You will need probably to use `NtResumeThread`, `NtWaitForSingleObject`, `RtlDestroyProcessParameters`, `RtlInitUnicodeString` additionally (and `RtlDosPathNameToNtPathName_U` would be helpful too). – Oleg Dec 31 '16 at 13:08
-
@nikos: You would easy to create the demo which uses the native API based on [the code example](http://www.rohitab.com/discuss/topic/28643-rtlcreateuserprocess/) and [this one](http://www.rohitab.com/discuss/topic/41379-running-native-applications-with-rtlcreateuserprocess/). If you would have some problems you can ask new question and I'll post the corresponding code example in my answer. – Oleg Dec 31 '16 at 13:09
-
I tried your undocumented stuff (runnt sample) but still no go. I tried a simple EXE with path length 307 characters and it fails in the call to RtlCreateUserProcess, regardless of the system LongPathsEnabled setting. In fact your sample cannot even start a plain process :) – nikos Jan 01 '17 at 17:33
-
@nikos: First of all you should change your ton if you want that somebody helps you. Seconds you should post *separate question* where you describe your problems and your current attempts more detailed. The current old answer is about "CreateProcess and ShellExec differences" and you have another question. – Oleg Jan 01 '17 at 20:55
-
I am not seeking for an answer. You made a wrong assertion above and I am correcting you. You cannot start a process > MAX_PATH, period – nikos Jan 02 '17 at 08:28
CreateProcess
returns the handle and id for the started process and it's main thread in the PROCESS_INFORMATION
structure

- 450
- 5
- 10