Solved

Win32 API question

Posted on 1998-09-05
7
226 Views
Last Modified: 2010-04-06
I have the handle of an application running outside of my delphi 3.0 application. How can I check when that application is finished from inside of my delphi application?
0
Comment
Question by:fh1
  • 5
  • 2
7 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1338755
There are many methods to do that and here is one of them,,,,,

In OnTimer()
var
  H : HWND;
begin
  H := FindWindow(nil, 'Untitled - Notepad');
  if H = 0 then
  begin
    ShowMessage('The application has already finished!');
    //Do your code here.....
    Timer1.Enabled := False;//If it's only for once..otherwise don't disable the timer...
  end;
end;

Regards,
Viktor Ivanov

0
 

Author Comment

by:fh1
ID: 1338756
To Viktor Ivanov:
I don't have the name of the window, but I have the handle of it.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338757
Here is the second method and more official, with a little more code....
Params -
   Filename - the filename of the app to execute and wait for.....
    Visibility - SW_SHOW, SW_HIDE, SW_SHOWNORMAL........
--------------
How do I execute a program and have my code wait until it is finished?

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,                   { pointer to STARTUPINFO }
    ProcessInfo) then Result := -1 { pointer to PROCESS_INF }

  else begin
    WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess,Result);
  end;
end;
------------
Regards,
Viktor Ivanov
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 10

Expert Comment

by:viktornet
ID: 1338758
If you want use the method I just gave otherwise if you want to use the first method here is how if you have the handle.....

var
  H : HWND;
begin
  H := //the handle you already know of your application.....
  if H = 0 then
  begin
    ShowMessage('The app is already running');
    //Do you job here.....
  end;
end;
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338759
oopssss....see the line that says ShowMessage('The app is already running');...this is suppose to be ShowMessage('The app is already finished');
sorry for the wrong code ;)

Regards,
Viktor Ivanov
0
 

Author Comment

by:fh1
ID: 1338760
Since I only know the handle of the window, I have to find the name of the window and then check in a loop if the handle of this window become zero.
0
 
LVL 10

Accepted Solution

by:
viktornet earned 50 total points
ID: 1338761
That's not true...You don't need the name if you know the handle. Just use the method I told you and that's all...

Regards,
Viktor Ivanov
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

772 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