ShellExecute block until executable is launched

Posted on 2007-08-09
Last Modified: 2008-03-21

I'm currently using ShellExecute to execute an exe. I am looking for a way to make sure that the ShellExecute call does not return until the call has been completed..

ie : if you use ShellExecute as

long r = (long) ShellExecute(NULL,_T("open"),"myexe.exe", NULL, NULL, SW_SHOWNORMAL);

It will launch the myexe.exe but will not wait for it to complete. Is there any way for it to wait ?
Question by:oddbin
    LVL 22

    Expert Comment

    I suspect ShellExecute returns you  a process ID, so you can probably do a processwait or processinquiry until that process goes away?

    LVL 8

    Expert Comment

    Here is an older solution that might work for you. Its basically the same as grg99's.
    LVL 1

    Accepted Solution

    #include <windows.h>
    #include <shellapi.h>
    #include <tchar.h>

    //      Function returns TRUE on success, and FALSE on failure
    //      dwRet - is set to the process return code on success, and the result of GetLastError() on failure
    //  nShow - is SW_SHOW ect...
    //  tcWorkingDir - string of the working directory
    //      tcExe - is the excutable name
    //      tcArgs - are arguments to the executable
    BOOL ShellExecuteWait( DWORD& dwRet , int nShow , TCHAR* tcExe , TCHAR* tcWorkingDir = NULL , TCHAR* tcArgs = NULL  )
          BOOL bRet = FALSE;
          SHELLEXECUTEINFO sexi = {0};
          sexi.cbSize = sizeof( SHELLEXECUTEINFO );
          // No Parent ( Note this is just for the example )
          sexi.hwnd = NULL;
          // Set flag so the hProcess handle is initialized by the call to ShellExecuteEx
          sexi.fMask = SEE_MASK_NOCLOSEPROCESS;
          sexi.lpDirectory = tcWorkingDir;
          sexi.lpFile = tcExe;
          sexi.lpParameters = tcArgs;
          sexi.nShow = nShow;
          // Returns Non-Zero on success
          if( ::ShellExecuteEx( &sexi ) )
                DWORD dwSignaled = ::WaitForSingleObject( sexi.hProcess , INFINITE );
                // WAIT_OBJECT_0 is success for the event trigger
                if ( dwSignaled == WAIT_OBJECT_0 )
                      // Returns Non-Zero on success
                      if( ::GetExitCodeProcess( sexi.hProcess , &dwRet ) )
                            bRet = TRUE;
          dwRet = ::GetLastError( );
          return bRet;
    void main()
          DWORD dwRet;
          ShellExecuteWait( dwRet , SW_SHOW , _T("notepad.exe") , _T("C:\\windows") , _T("C:\\test.txt") );

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
    If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
    Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    729 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