I was trying to launch exe files and shortcuts that point to exe files (shortcuts may have command line arguments on them). I was looking for a winapi way to launch them such that the return value is not cared/waited for. I have only found ways where it either blocks till the launched file exits, or asynchronously waits till it exits. Is there a way that just triggers it to launch and doesn't care beyond that? Basically like the user double click that exe/shortcut.
Asked
Active
Viewed 126 times
0
-
1Have a look at [this][1] answer, it seems you want to do the same thing. [1]: http://superuser.com/a/591084 – cdanzmann Mar 27 '15 at 06:31
-
Many thanks @cdanzmann but it wasn't what I was hoping for. Jon's answer below was perfect. – Blagoh Mar 27 '15 at 08:45
1 Answers
2
Easy using ShellExecuteEx
:
SHELLEXECUTEINFO sei = {0};
sei.cbSize = sizeof(sei);
sei.lpFile = L"c:\\path\\to\\your\\shortcut.lnk";
sei.nShow = SW_SHOWNORMAL;
ShellExecuteEx(&sei);

Jonathan Potter
- 36,172
- 4
- 64
- 79
-
I had some curiosity Jon. I was wondering why `ShellExecute` could not do this "async" mode of trigger and don't care, it's not made clear on the docs for either ShellExec or ShellExecEx, how did you know about this? Thanks – Blagoh Mar 27 '15 at 08:58
-
Also I was getting access denied (GetLastError of 5) so I read here: http://blogs.msdn.com/b/oldnewthing/archive/2010/11/18/10092914.aspx i then tried this `CoInitializeEx(NULL,COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);` before the call to the above but it continued to give me error of 5, any ideas? Thanks once again – Blagoh Mar 27 '15 at 09:41
-
2@Blagoh: `ShellExecuteEx` has a return value. It cannot return, until it knows, whether the operation succeeded or not. In this case, the API has to parse shortcuts, potentially load shell extensions (that's why you need to initialized COM on the calling thread), and eventually perform the `CreateProcess` operation. Only then is it possible to return error/success. – IInspectable Mar 27 '15 at 11:57
-
Thank you @IInspectable, won't createProcess make my launching application wait for a response from its exit? Also because ShellExec is giving me access denied I won't be able to get to CreateProcess no? – Blagoh Mar 27 '15 at 17:56
-
@Blagoh: `CreateProcess()` exits as soon as the new process has been created. If you want to wait for a result from the new process, you have to do so separately. So you would simply omit that wait in your code. `ShellExecute()` does a lot of extra work before it reaches `CreateProcess()`, so it is slower. You can use `CreateProcess()` directly to launch a `.exe` file, but you need to use `ShellExecute/Ex()` to launch a `.lnk` file, unless you use `IShellLink` to retrieve the shortcut's target and launch it yourself. – Remy Lebeau Mar 27 '15 at 18:32
-
@Blagoh: After the call to `ShellExecuteEx` what value is found in the `hInstApp` field? – Jonathan Potter Mar 27 '15 at 19:34
-
Thanks @JonathanPotter, I am getting `0x5` in my `hInstApp` field. I paste binned the struct after running here: http://pastebin.com/tvZGx7cm – Blagoh Mar 27 '15 at 20:40
-
1ShellExecuteEx seems to use error 5 as a general "catch all" error, so it literally could be anything. Your pastebin looks odd - fields like cbSize and fMask aren't 64 bit fields and yet they're shown as UInt64. And I think your cbSize is wrong - it should be 60 for a 32 bit build, and something like 104 in a 64 bit build - not 56 either way. See https://msdn.microsoft.com/en-us/library/windows/desktop/bb759784%28v=vs.85%29.aspx for the proper definition of a `SHELLEXECUTEINFO` structure. – Jonathan Potter Mar 27 '15 at 20:52
-
@Jonathan, you were right! Thank you! I had forgot the `hProcess` field of the structure! Thank you!! :) Btw is there a special format for the paths? Because if I supply `C:\blah\blah.exe` it throws this message: `Windows cannot find 'C'. Make sure you typed the name correctly` – Blagoh Mar 28 '15 at 07:25
-
Doh found the issue, I didn't put a ShellExecuteExW, i left off the W and was using unicode. my goodness lol thanks all – Blagoh Mar 28 '15 at 08:13