Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GetExitCodeProcess

Posted on 1998-08-08
3
Medium Priority
?
224 Views
Last Modified: 2010-04-04
How can i create a timer for my application to call GetExitCodeProcess() . If the function returns 0
and the lpExitCode is STILL_ACTIVE , the program has not yet been terminated . If the function returns true , then the lpExitCode has the returned error value ( errorlevel ) ,

Can anyone help me.. i need a source please..!! THANKS
0
Comment
Question by:ichen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 300 total points
ID: 1361731
Hi ichen,

Is this what you want?

function WinExecAndWait32(FileName:String; Visibility :
integer):integer;
var
  zAppName:array[0..512] of char;
  zCurDir:array[0..255] of char;
  WorkDir:String;
  StartupInfo:TStartupInfo;
  ProcessInfo:TProcessInformation;
begin
  StrPCopy(zAppName,FileName);
  GetDir(0,WorkDir);
  StrPCopy(zCurDir,WorkDir);

  FillChar(StartupInfo,Sizeof(StartupInfo),#0);
  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 new environment block }
    nil, { pointer to current directory name }
    StartupInfo, ointer to STARTUPINFO }
    ProcessInfo) then Result := -1 { pointer to PROCESS_INF }
  else begin
    WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess,Result);
  end;
end;

Zif.

Regr
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1361732
Hi,
ZifNab's answer is the way to do it. Just execute his code in a thread so that your main app doesn't lock.
If you still want to do it the timer way; here's an example I just whipped together:

type
  TProcessWatch = class( TTimer )
  private
    FhProcess : THandle;
    FiExitCode: Integer;
    FbRunning : Boolean;
    FOnExit   : TNotifyEvent;
    procedure FWatch(Sender: TObject);
    property  OnTimer;
  public
    constructor Create( AOwner: TComponent; Process: THandle; Suspended: Boolean );
    procedure   Start;
    procedure   Stop;
    property    Handle  : THandle read FhProcess write FhProcess;
    property    ExitCode: Integer read FiExitCode;
    property    Running : Boolean read FbRunning;
    property    OnProcessExit: TNotifyEvent read FOnExit write FOnExit;
  end;

.

procedure TProcessWatch.FWatch(Sender: TObject);
var
  iExitCode: Integer;
begin
  if ( GetExitCodeProcess( FhProcess, iExitCode ) )
  then begin
    FiExitCode := iExitCode;
    FbRunning  := ( iExitCode = STILL_ACTIVE );
    if ( not FbRunning ) and Assigned( FOnExit )
    then FOnExit( Self );
  end else begin
    FiExitCode := -1;
    FbRunning  := False;
  end;
end;

constructor TProcessWatch.Create( AOwner: TComponent; Process: THandle; Suspended: Boolean );
begin
  inherited Create( AOwner );
  inherited OnTimer := FWatch;
  FhProcess         := Process;
  OnTimer( Self ); // get initial status
  Enabled := not Suspended;
end;

procedure   TProcessWatch.Start;
begin
  Enabled := True;
end;

procedure   TProcessWatch.Stop;
begin
  Enabled := False;
end;

To use it:

procedure TForm1.OnExitProc(Sender: TObject);
begin
  Windows.Beep( 1000, 1000 );
  ( Sender as TProcessWatch ).Free;
end;

.

procedure TForm1.NotepadButtonClick(Sender: TObject);
var
  Command    : string;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  Command := 'notepad';
  FillChar(StartupInfo, Sizeof(StartupInfo),#0);
  StartupInfo.cb := SizeOf( StartupInfo );
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := SW_SHOWNORMAL;
  if CreateProcess(nil,
    PChar(Command),           { 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 new environment block }
    nil,                      { pointer to current directory name }
    StartupInfo,              { pointer to STARTUPINFO }
    ProcessInfo)              { pointer to PROCESS_INF }
  then begin
    with TProcessWatch.Create( Self, ProcessInfo.hProcess, True ) do begin // create suspended
      OnProcessExit := OnExitProc;
      Interval := 100; // faster check than def
      Start;
    end;
  end;
end;

Not pretty, but working!

/// John
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361733
you whipped good John :-), Zif.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

719 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