Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ShellExecute(…)

Status
Not open for further replies.

d00ape

Programmer
Apr 2, 2003
171
SE
I’m successfully manage to run the zipprogram gzip.exe programmatically like this:

ShellExecute( NULL, "open", "C:\\gzip.exe", strParam, "C:\\Program Files\\Ceco", NULL ) ) )
//Here I want to sleep…. Until the new process has ended.

How do I after this call wait until gzip.exe has returned. (The file that gzip.exe creates do I want to rename.)
 
Use CreateProcess & WaitForSingleObject instead.

Code snippet from MSDN:
Code:
#include <windows.h>
#include <stdio.h>

void main(void)
{
   PROCESS_INFORMATION pInfo;
   STARTUPINFO         sInfo;
   DWORD               exitCode;

   sInfo.cb              = sizeof(STARTUPINFO);
   sInfo.lpReserved      = NULL;
   sInfo.lpReserved2     = NULL;
   sInfo.cbReserved2     = 0;
   sInfo.lpDesktop       = NULL;
   sInfo.lpTitle         = NULL;
   sInfo.dwFlags         = 0;
   sInfo.dwX             = 0;
   sInfo.dwY             = 0;
   sInfo.dwFillAttribute = 0;
   sInfo.wShowWindow     = SW_SHOW;

   if (!CreateProcess(NULL,
                "command.com /c dir c:\\*.bat",
                      NULL,
                      NULL,
                      FALSE,
                      0,
                      NULL,
                      NULL,
                      &sInfo,
                      &pInfo)) {
      printf("ERROR: Cannot launch child process\n");
      exit(1);
   }

   // Give the process time to execute and finish
   WaitForSingleObject(pInfo.hProcess, 5000L);

   if (GetExitCodeProcess(pInfo.hProcess, &exitCode))
   {
      switch(exitCode)
      {
         case STILL_ACTIVE: printf("Process is still active\n");
                            break;
         default:           printf("Exit code = %d\n", exitCode);
                            break;
      }
   }
   else {
      printf("GetExitCodeProcess() failed\n");
   }
}

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
or even better

ShellExecuteEx(...)

like

SHELLEXECUTEINFO lpExec;
HINSTANCE hInst = NULL;

lpExec.cbSize = sizeof(SHELLEXECUTEINFO);
lpExec.fMask = SEE_MASK_NOCLOSEPROCESS ;
lpExec.hwnd = NULL;
lpExec.lpVerb = _T("OPEN");
lpExec.lpFile = "...";
lpExec.lpParameters = NULL;
csParams.GetLength() );
lpExec.lpDirectory = NULL;
lpExec.hInstApp = hInst;
lpExec.nShow = SW_SHOW;


if ( ! ShellExecuteEx( &lpExec ) )
{
CloseHandle( lpExec.hProcess );

return FALSE;
}

// wait for process to finish
WaitForSingleObject( lpExec.hProcess, INFINITE );

CloseHandle( lpExec.hProcess );
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top