Solved

Find and close an app from a delphi program.

Posted on 1998-11-19
6
192 Views
Last Modified: 2010-05-18
How would I go about finding and closing another program from within a delphi program(D4)? Would I use something like GetWindowText to locate the program I wish to close?

Thanks
0
Comment
Question by:LostOne
[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
6 Comments
 
LVL 1

Expert Comment

by:mpagnan
ID: 1347395
What do you mean by 'find another program'? Another program that is running?
0
 

Accepted Solution

by:
kashif063098 earned 200 total points
ID: 1347396
If you know the program that is running and you want to close it then do the following window api calls

var
  hWindow:HWND;
begin
  hWindow:=FindWindow('',ApplicationWindowTitle);
 
  DestroyWindow(hWindow);
end;

any question please ask!

 

0
 

Author Comment

by:LostOne
ID: 1347397
The DestroyWindow doesn't seem to work. I was able to get the hwnd of MIRC but the application didn't close when I passed the hwnd to DestroyWindow.

Any ideas?
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 4

Expert Comment

by:BoRiS
ID: 1347398
LostOne

Here is how I did it in my application

PostMessage(FindWindow(Nil, 'window caption'), WM_QUIT, 0, 0);

Later
BoRiS
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1347399
LostOne,

DestroyWindow is no good solution. If you want the box "file is not saved. Save it now?" of the program you want to close to appear, you have to send a WM_CLOSE message to the main form. If you don't want this box (that means the opened file is DEFINITELY NOT saved) you should use BoRiS' solution, though I'm not sure if you can send WM_QUIT to a window. Usually I'm sending it to the main thread by using PostThreadMessage.
However, all these "FindWindow" solutions have one big disadvantage: If the title changes (e.g. if another file is loaded), you'll have a lot of problems.
So perhaps you should use the toolhelp functions. But this is a little bit more complex and it will work only with win95/98/NT4SP4/NT5, not with NT3 or NT4 without service pack 4.
Look at the following example, which gives you the processID and all threadIDs from the desired application. You can give in the processID or the exepath/name.
Now you can send a WM_QUIT to all the threadIDs using PostThreadMessage or you can enumerate all windows of the threads using EnumThreadWindows and then send them a WM_CLOSE.

procedure FindProcess(pid: dword; exeFile: string);
var c1,c2 : dword;
    pe    : TProcessEntry32;
    te    : TThreadEntry32;
begin
  c1:=CreateToolHelp32Snapshot(TH32CS_SnapProcess,0);
  try
    pe.dwSize:=SizeOf(pe);
    if Process32First(c1,pe) then
      repeat
        if (pe.th32ProcessID=pid) or IsTextEqual(pe.szExeFile,exeFile) then begin
          c2:=CreateToolHelp32Snapshot(TH32CS_SnapThread,0);
          try
            te.dwSize:=SizeOf(te);
            if Thread32First(c2,te) then
              repeat
                if te.th32OwnerProcessID=pe.th32ProcessID then begin
                  // Now you have to processID and all thread's IDs...
                end;
              until not Thread32Next(c2,te);
          finally CloseHandle(c2) end;
        end;
      until not Process32Next(c1,pe);
  finally CloseHandle(c1) end;
end;

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1347400
Hey, LostOne, you said kashif's suggestion didn't work!? So why do give him the points???
IMHO the points should have gone to BoRiS or me!
It's not very motivating for us experts, when others get our points...   :-(((
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month5 days, 21 hours left to enroll

626 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