Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2007 Jul : WinExecAndWait32V2 under Vista

www.cryer.info
Managed Newsgroup Archive

WinExecAndWait32V2 under Vista

Subject:WinExecAndWait32V2 under Vista
Posted by:"Rimvydas Paulavicius" (pauli..@post.5ci.lt)
Date:Wed, 11 Jul 2007 11:03:50

Does anybody use function WinExecAndWait32V2 under Vista?
I have a problem - desired procedure starts, works and finishes, but
control DOES NOT return to the main program. It became "not responding".

TIA,
Rimvydas

function WinExecAndWait32V2( const FileName, CurrDir: string;
Visibility: integer ): DWORD;
{ V1 by Pat Ritchey, V2 by P.Below }
   procedure WaitFor( processHandle: THandle );
      var
         msg: TMsg;
         ret: DWORD;
      begin
         repeat
            ret := MsgWaitForMultipleObjects(
                      1,             { 1 handle to wait for }
                      processHandle, { the handle }
                      FALSE,         { wake on any event }
                      INFINITE,      { wait without timeout }
                      QS_PAINT or    { wake on paint messages }
                      QS_SENDMESSAGE { or messages from other threads }
                   );
            if ret = WAIT_FAILED then Exit; { can do little here }
            if ret = (WAIT_OBJECT_0+1) then begin
{ Woke on message, process paint messages only. Calling
  PeekMessage gets messages sent from other threads processed. }
               while PeekMessage( msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE
) do
                  DispatchMessage( msg );
            end;
         until ret = WAIT_OBJECT_0;
      end;{ WaitFor }
   var
      zAppName:    array[ 0..512 ] of char;
      zAppPath:    array[ 0..512 ] of char;
      StartupInfo: TStartupInfo;
      ProcessInfo: TProcessInformation;
      {ProHandle: THandle;}
      Buf1, Buf2: array[ 0..80 ] of char;
   begin{ WinExecAndWait32V2 }
      StrPCopy( zAppName, FileName );
      StrPCopy( zAppPath, CurrDir );
      FillChar( StartupInfo, SizeOf(StartupInfo), $00 );
      StartupInfo.cb := SizeOf(StartupInfo);
      StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := Visibility;
      if not CreateProcess( NIL,
          zAppName,                 { pointer to command line string }
          NIL,                      { pointer to process security
attributes }
          NIL,                      { pointer to thread security
attributes }
          FALSE,                    { handle inheritance flag }
          CREATE_NEW_CONSOLE or     { creation flags }
          NORMAL_PRIORITY_CLASS,
          NIL,                      { pointer to environment block }
          zAppPath,                 { pointer to current directory name
}
          StartupInfo,              { pointer to STARTUPINFO }
          ProcessInfo )             { pointer to PROCESS_INF }
       then
          Result := DWORD( -1 )     { failed, GetLastError has error
code }
       else begin
         WaitFor( ProcessInfo.hProcess );
          GetExitCodeProcess( ProcessInfo.hProcess, Result );
          CloseHandle( ProcessInfo.hProcess );
          CloseHandle( ProcessInfo.hThread );
       end;
   end;{ WinExecAndWait32V2 }

Replies:

www.cryer.info
Managed Newsgroup Archive