4

I am launching a process from ShellExecuteEx, and I really need to get the ProcessID (It's part of the requirement of this class).

Somehow all the important SHELLEXECUTEINFO returns null. So for example if I use this code:

exInfo.lpVerb = "open";
exInfo.lpFile = "C:\\Windows\\system32\\cmd.exe";
exInfo.nShow  = 5;

ShellExecuteExA(exInfo);

It launched CMD.exe. But now I need to get it's PID. exInfo.hwnd is returning 0, and exInfo.hProcess is returning null. Is this normal behaviour?

I don't really want to resort to using CreateProcess(), because my function should also be able to launch documents like "C:\doc1.docx". This is just a method, in which I cannot predict what is going to be launched (So I cannot know the window title/classname from beforehand, get the hWnd from there and then get the PID).

Could somebody kindly point out my mistake? Thanks.

David
  • 15,652
  • 26
  • 115
  • 156

1 Answers1

2

You need to set a flag (SEE_MASK_NOCLOSEPROCESS) in exInfo.fMask

Luke
  • 11,211
  • 2
  • 27
  • 38
  • 3
    Be aware that `ShellExecuteEx` can returns success without creating a process. (For example, if the handler is a single-instance app.) – Raymond Chen Oct 01 '11 at 14:33