Solved

Find and close an app from a delphi program.

Posted on 1998-11-19
6
191 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
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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

751 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