Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Find and close an app from a delphi program.

Posted on 1998-11-19
6
Medium Priority
?
196 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

783 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