Solved

Find and close an app from a delphi program.

Posted on 1998-11-19
6
180 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
6 Comments
 
LVL 1

Expert Comment

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

Accepted Solution

by:
kashif063098 earned 200 total points
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

12 Experts available now in Live!

Get 1:1 Help Now