Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close on application from another application 1

Status
Not open for further replies.

GCTIberica

Programmer
Jun 20, 2003
30
ES
Hi
Does anyone know how to close one application from another application? I don´t want to kill the process - what I would like to do is for the application to do whatever it has to do on the OnClose event before it is closed!

Thanks in advance
 
hi

Have at look at my example code in thread102-610666, this sends a WM_CLOSE message to the app - The app should then perform any necessary close routines before exiting.

lou

 
Hi
Perhaps a really stupid question, but the line of code
EnumWindows(@EnumerateWindows,0);
will not run.
It says : Variable required
Do you know why?
Thanks
 
Did you put the function declaration in the Interface part of the unit?

function EnumerateWindows(hWnd: HWND; lparam:LPARAM):Bool; stdCall;

 
Hi
Sorry-wasn´t working yesterday afternoon!
I haven´t got it working... this is what I have done!

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
function EnumerateWindows(hWnd: HWND; lparam:LPARAM):Bool; stdCall;
procedure Button1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumerateWindows,0);
end;

function TForm1.EnumerateWindows(hWnd: HWND; lparam:LPARAM):Bool;
var
ClassName, TheText : Array [0..255] of char;
sName : string;
begin
Application.ProcessMessages;
GetClassNAme(hWnd,Classname, 255);
if GetWindowText(hWnd, TheText, 255) > 0 then
begin
sName := StrPas(TheText);
if pos('NOTEPAD',UpperCase(sName)) > 0 then
postMessage(FindWindow(ClassName, TheText), WM_CLOSE, 0, 0);
end;
end;

end.
 
hi

I've changed your mistakes(in blue), here's the working code:-

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
{ Public declarations }
end;

function EnumerateWindows(hWnd: HWND; lparam:LPARAM):Bool; stdCall;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumerateWindows,0);
end;

function EnumerateWindows(hWnd: HWND; lparam:LPARAM):Bool;
var
ClassName, TheText : Array [0..255] of char;
sName : string;
begin
Application.ProcessMessages;
GetClassNAme(hWnd,Classname, 255);
if GetWindowText(hWnd, TheText, 255) > 0 then
begin
sName := StrPas(TheText);
if pos('NOTEPAD',UpperCase(sName)) > 0 then
postMessage(FindWindow(ClassName, TheText), WM_CLOSE, 0, 0);
end;
end;

end.

lou

 
Thanks so much
That works like a dream!!!!!!

THANKSSSSSSSSS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top