ShellExecute() takes too long

Posted on 2007-07-31
Last Modified: 2013-11-20
I have a ShellExecute() call in an application that opens Firefox.exe:

            hdl = ShellExecute(
            NULL,            // parent window
            "open",            // verb: open = "double-click"
            "C:\\Program Files\\Mozilla Firefox\\firefox.exe",      
            NULL,            // no parameters passed to program being opened
            NULL,            // default directory
            SW_NORMAL);                  // flags: open
when a button on an MFC dialog box is selected.  The problem is that it takes about 30 seconds to open Firefox this way, while if I double click a shortcut on my desktop that's set to

                                            C:\Program Files\Mozilla Firefox\firefox.exe

Firefox opens right away.  What's taking ShellExecute() so long and how can I open Firefox faster from my application?

Question by:steva
    LVL 22

    Expert Comment

    >> how can I open Firefox faster from my application?

    I just made a test and at my side it just open normally without any delay. However you may try to execute same using WinExec() or CreateProcess() API to check if that makes any difference to ensure if its just shellexecute issue.


    WinExec("C:\\Program Files\\Mozilla Firefox\\firefox.exe",SW_SHOWNORMAL);



    CreateProcess(  NULL,"C:\\Program Files\\Mozilla Firefox\\firefox.exe",NULL, NULL,FALSE,0,NULL,NULL,&si,&pi )

    LVL 22

    Expert Comment

    just a NOTE :
    You've got the last parameter of ShellExecute wrong - it should be "SW_SHOWNORMAL"  instead of SW_NORMAL however that should not cause any problems.


    Author Comment

    It looks like winuser.h defines both as 1:

     * ShowWindow() Commands
    #define SW_HIDE             0
    #define SW_SHOWNORMAL       1
    #define SW_NORMAL           1
    #define SW_SHOWMINIMIZED    2
    #define SW_SHOWMAXIMIZED    3
    #define SW_MAXIMIZE         3
    #define SW_SHOWNOACTIVATE   4
    #define SW_SHOW             5
    #define SW_MINIMIZE         6
    #define SW_SHOWNA           8
    #define SW_RESTORE          9
    #define SW_SHOWDEFAULT      10
    #define SW_FORCEMINIMIZE    11
    #define SW_MAX              11

    I'll try your other ideas tomorrow.  Thanks.
    LVL 86

    Expert Comment

    >>The problem is that it takes about 30 seconds to open Firefox this way

    If that behaviour is the same when doubleclicking on a HTML file, there is a configuration issue. You cannot compare that to starting FF directly.

    Author Comment

    Double-clicking an html file opens up FireFox ok.  

    I think the problem is the way I've programmed this. When the application I've written first runs it checks the license and if it's still temporary it brings up a Buy.exe program that offers a chance to buy a permanent license.  The application starts us Buy.exe with ShellExecuteEx, passing the number of days left on the temporary license. The application can't continue until Buy.exe returns - either by the user deciding to continue with the temporary license or to purchase a permanent license - so the application waits with

          do {
                      GetExitCodeProcess(ShellExecuteInfo.hProcess, &ExitCode); //  check status of Buy.exe
                while(ExitCode == STILL_ACTIVE);

    If you look at Task Manager you can see that 99% of the CPU time is spent in this apolication  loop while Buy.exe is running so I suspect that Buy.exe is only getting about 1 out of every 100 CPU cycles to go off and start FireFox, hence the delay.

    Any suggestions on the correct way to code this?

    LVL 86

    Accepted Solution

    >>Any suggestions on the correct way to code this?

    Yes, change that to


    This will wait at 0% CPU load.

    BTW, you could also use

    DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
       STARTUPINFO         si;

       BOOL                bRes;

       DWORD               dwCode  =   0;

       MSG                           msg;

       ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

       si.cb           =   sizeof  (   STARTUPINFO);
       si.dwFlags      =   STARTF_USESHOWWINDOW;
       si.wShowWindow  =   SW_SHOWNORMAL;

       bRes    =   CreateProcess   (   NULL,

       while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                   while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                               DispatchMessage     (   &msg);

       GetExitCodeProcess  (   pi.hProcess,    &dwCode);

       CloseHandle (   pi.hProcess);
       CloseHandle (   pi.hThread);

       return  (   dwCode);

    which is quite a time-tested function here on EE.

    Author Comment



    works!  And I'll store away the other code in my toolbox.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Title # Comments Views Activity
    java ^ examples 8 48
    haveThree challenge 22 89
    sumHeights  challenge 17 50
    wordmultiple challenge 12 63
    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 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

    18 Experts available now in Live!

    Get 1:1 Help Now