C++, ShellExecute, forcing new app to front

Posted on 2011-04-21
Last Modified: 2012-05-11
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
Question by:PMH4514
    LVL 16

    Expert Comment

    Can you use SW_SHOWMAXIMIZED instead of SW_SHOWNORMAL?
    LVL 33

    Accepted Solution

    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().

    Author Comment

    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!
    LVL 33

    Expert Comment

    by:Todd Gerbert
    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.

    Author Comment

    thank you tgerbert - your original suggestion worked! I apologize for the delayed response.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now