C++, ShellExecute, forcing new app to front

In Visual C++ 6.0, I call:

      HINSTANCE hInst = ShellExecute(NULL, NULL, sLaunch, sParams, NULL, SW_SHOWNORMAL);

where sLaunch is the fully qualified path to the executable, and sParams are command line params.

This works, the application runs as it should.  The problem is that sometimes the app loads, but it's not got focus - its icon in the windows taskbar is flashing.. Other times, it actually loads and comes to the front as it should.

How can I force the launched application to come up to the front so the user doesn't then have to recognize there is a flashing icon in the footer and click it?

My platforms of concern are XP Pro, and Win7
Who is Participating?
Todd GerbertIT ConsultantCommented:
You could use CreateProcess instead of ShellExecute (if all you're doing is starting an EXE, and not e.g. pointing to a .doc file with the "open" verb).  CreateProcess will give you the process ID, which you can use in combination with EnumWindows() to get the HWND for the spawned application and call BringWindowToTop().
PMH4514Author Commented:
HookooDooKu - tried that, it didn't work.

tgerbert - that just might work. I'll give it a try, but will not be able to report back today due to scheduling. thanks!
Todd GerbertIT ConsultantCommented:
I was mucking about with this a bit today and it seems using SetWindowPos() with HWND_TOPMOST set seems to most reliably bring the child process's window to the foreground.  The only little caveat that I see is that a given process may have more than one top-level window, so it's possible (depending on the child process you're starting) you might not accurately be able to determine the hWnd.
PMH4514Author Commented:
thank you tgerbert - your original suggestion worked! I apologize for the delayed response.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.