Search code examples
c++windowswinapijava-native-interfacejna

Get PID from ShellExecute


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.


Solution

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