Solved

Win32 API question

Posted on 1998-09-05
7
228 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
[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
  • 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
Industry Leaders: 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!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

735 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