Link to home
Start Free TrialLog in
Avatar of billcch
billcch

asked on

ShellExecute and waiting the execute file finish

I have the following code

HANDLE hShell;
hShell = ShellExecute(NULL, "open", "test.exe", "parameter1 parameter2", "C:\\", SW_SHOWDEFAULT);
CloseHandle(hShell);
MessageBox(NULL, "text.exe finished", "myProg", MB_OK);

But I would like the message "text.exe finished" display after test.exe has been finished.
Please help me....
Avatar of jkr
jkr
Flag of Germany image

You cannot do that with 'ShellExecute()' - use e.g.

DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
{
   STARTUPINFO         si;
   PROCESS_INFORMATION pi;

   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,
                                   pszCmd,
                                   NULL,
                                   NULL,
                                   TRUE,
                                   NORMAL_PRIORITY_CLASS,
                                   NULL,
                                   NULL,
                                   &si,
                                   &pi
                               );

   while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                                                                   &pi.hProcess,
                                                                   FALSE,
                                                                   INFINITE,
                                                                   QS_ALLINPUT
                                                               )
           )
           {
               while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                       {
                           DispatchMessage     (   &msg);
                       }
           }

   GetExitCodeProcess  (   pi.hProcess,    &dwCode);

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

   return  (   dwCode);
}

and call it like

ExecuteAndWaitForCompletion ( "test.exe parameter1 parameter2",);
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Um, and a lil' correction - the call above should have read

ExecuteAndWaitForCompletion ( "test.exe parameter1 parameter2");
Avatar of billcch
billcch

ASKER

Thank you so much....
one small question...
I used to use std::string,
what different between std::string and LPSTR?
Thanks...
>>what different between std::string and LPSTR?

'LPSTR' is a plain 'C' style string. So, you could just use

std::string str("test");
LPSTR pstr = str.c_str();
Avatar of billcch

ASKER

thank you so much