We help IT Professionals succeed at work.

Running a program from a program

ZeinerRJ asked
Last Modified: 2011-09-20
I have an application that minimizes itself and then launches another program with the ShellExecute command.  Is there any way I can find out when the second program is done so I can Restore the main window of the first app?  Please note that I have no access to the second app, so I can't have it send a message back.

Thanks for any help
Bob Z.
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
Ignore this comment of mine
--> You may also want to remove the "_declspec(dllexport)"

This is req'd I misread the code.

Read it right first time!!  The _declspec is req'd if you are exporting the function from a DLL.  If you simply embed the code into your app, then you don't need it.  

So the original post was correct, and my 2:04PM comment is rubbish

What you need to do is open a handle to the process with OpenProcess() and then wait on that handle with WaitForSingleObject().  When the process termiantes, WaitForSingleObject will return.  (You can do more fancy things if you want your application to "stay alive" while waiting, but that is the general idea.

The problem is that to use OpenProcess() you need the process's ID. Unfortunately you don't have the process's ID, you have its instance handle.  There is no way to convert to the ID that I know of.  I believe that is a security measure.  

To get around this, you could open the process with CreateProcess()  That way you can get the process's ID and use it to open a handle for the proccess.  However, CreateProcess needs the name and path of the executable to be run.  To get around this use FindExecutable().  This finds the executable that ShellExecute() would run for a specified file.

Answers2000, that's not really what he needs.  The root of the problem is that he doesn't have an executable file name to pass to CreateProcess() (or your procedure) and that ShellExecute() doesn't return the information needed to monitor the process started.

ZeinerRJ, the answer to your question is ShellExecuteEx().
See the documentation for the SHELLEXECUTEINFO structure.  Especially the hProcess

Alex is that a handle to the process, as in a the return value of OpenProcess(), or is it the handle to the process's instance, as in the return value of ShellExecute()?  

The return value of ShellExecute() is totally useless bacause instance handles are only valid in the same address space.  Ergo, first option.

Right, I understand that.  That is why I thought it was necessary to find the EXE with FindExecurable() and then run it with CreateProcess(), so you can get a process handle.  

My question is:  "is the handle returned by ShellExecuteEx() really a process handle, or is it an instance handle?"  Since ShellExecute() returns an instance handle, then I would be surprised if ShellExecuteEx() returned a process handle instead.  

Process handle.


I thank everyone for their comments.  I should have mentioned that I was given a copy of MSVC 1.5 and am trying to learn some C++ on my own until I can take a class.  (D'Ohh!!!)

However, all the comments are interesting, and will certainly be of use.

Thanks again, and if anyone has any new ideas, let me know.

Bob Z.

Bob, next time please give us the full information so we can supply *correct* solutions.
My comments regarding ShellExecute() and ShellExecuteEx() are only valid for Win32.  Since MSVC 1.x generates Win16 code, you can only file them for future reference...


Sorry Alexo, when I posted, I was concentrating on the problem so much that I failed to give proper background info.  However, all I'm doing with 1.5 is trying to give myself a leg up  for when I'll get to take a class in a month or so.  At that time, I'll be using VC 5, or 6, so all these comments are indeed filed for future use.  You have not wasted your time!

Thanks again.
Bob Z.

Is this class in programing standard C++ or in windows?  A standard C++ (introductory) class will not cover anything like this.  It won't teach anything specific to windows or any other operating system.

Todd is right.  Better get familiar with virtual methods, multiple inheritance, operator overloading and other mumbo jumbo...
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.