Solved

Win32 API question

Posted on 1998-09-05
7
223 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now