We help IT Professionals succeed at work.

How to close a application from other  application

petershaw8
petershaw8 asked
on
Medium Priority
164 Views
Last Modified: 2010-04-05
I write application A and application B. Application A knows Application B's name(title), handle. If Application knows application is running, how can Application A close application B?

peter
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Hi Peter,

PostMessage(h, WM_CLOSE, 0, 0);

where Handle is the h of the main window of app B. Next example closes a window with 'App B title' if any.

procedure TForm1.Button2Click(Sender: TObject);
var
 h : HWND;
begin
 h := FindWindow(nil, 'App B title');
 if h <> 0 then
   PostMessage(h, WM_CLOSE, 0, 0);
end;

Regards, Geo

Commented:
HI, Petershaw8,
If you have only the ProcessID( not the Handle!) of the process and don't have the handle of the main window and can't get him (for example you don't know the title of the main window or the application is hiding) then you can use the following procedure:

functin TFormMain.TerminateTheProcessFromItsID(
NeededProcessID: DWORD): boolean;
var lpExitCode: DWORD;
    h: THandle;
begin
 Result:=False;
//
   h:=OpenProcess(PROCESS_TERMINATE,
                  False,
                  NeededProcessID);
//
   if h=0
    then EXIT;
//
   if not GetExitCodeProcess(h, lpExitCode)
    then EXIT;
//
   Result:=TerminateProcess(h, lpExitCode);
end;

Sincerely,
Nestorua.
Hi,
For a good explanation of what you want see this:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=52&lngWId=7


"Closing other applications from your application"

Rhat should get you there:-)
T.
Hi again,

Here is a very simple way to close an
application from your own:
....................................................
procedure TForm1.Button1Click(Sender: TObject);
var
  h: HWND;
begin
  h := FindWindow(nil, 'untitled - paint');
  if h <> 0 then PostMessage(h, WM_CLOSE, 0, 0);
end;

end.
................................................
where I have inserted the 'untitled - paint'
example, substitute the unique (exact)main form
window caption of the application you wish to close.
I used the default window in MS Paint for the example.

This should do what you want assuming you know the
application window caption.
Good luck
Tongalite.
Arrrrrrhhhhh!!!!!
disregard that last post... I just checked the
other comments and my post is the same as geo's
Doh!!! Sorry about that... I must be in a coma:)
T.

Author

Commented:
Hi,
I believe all these suggetions work. Thank u very much.
Nestorua, what is processID? How can I get it?

Peter
CERTIFIED EXPERT
Commented:
Hi,
How to get ProcessID from a window handle:

procedure TForm1.Button1Click(Sender: TObject);
var
  ProcessID: DWORD;
  h: HWND;
begin
  h := FindWindow(nil,'Untitled - Notepad');
  if IsWindow(h) then begin
    GetWindowThreadProcessId(h,@ProcessID);
    ShowMessage('Process ID = '+IntToStr(ProcessID));
  end;
end;

Regards, Geo

Explore More ContentExplore courses, solutions, and other research materials related to this topic.