Solved

Find and close an app from a delphi program.

Posted on 1998-11-19
6
184 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

15 Experts available now in Live!

Get 1:1 Help Now