Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

hooking events of an object by handle in delphi

342 views
Skip to first unread message

kemal

unread,
May 31, 2008, 7:15:44 AM5/31/08
to
is it possible to hook an external windowtext whenever it is changed ?
I wantto trigger something whenever a windowtext(which is in external
application's window)

WM_GETTEXT I can get clearly with this number.its okay.

but also I dont wantto use a timer to get updated texts from that external
application.

I just wantto hook an external applications log window text in to my
application.


any clue ?
thanks.


kemal

unread,
May 31, 2008, 8:01:45 AM5/31/08
to
CallWindowProc or GWL_WNDPROC doesnt work for external window handles
am I wrong ?


kemal

unread,
May 31, 2008, 8:14:39 AM5/31/08
to
ah-Ha I understood.
there must be a dll between external application and my own application.


kemal

unread,
May 31, 2008, 10:04:38 AM5/31/08
to
I have 2 projects 1 is exe other is dll
but my Form1.caption never changes
where I am wrong ? please HELP

----------------DLL
library Project2;

uses
SysUtils,
Windows,
Classes,Messages;


{$R *.res}
var
hNextHook:HWND;
IniProc:Pointer;

function
CallWndProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):Longint;stdcall;
var
Msg:PCWPStruct;
begin
Msg :=pointer(lParam);
if IniProc<>nil then
CallWindowProc(IniProc,Msg^.hwnd,Msg^.message,Msg^.wParam,Msg^.lParam);
Result := CallNextHookEx(hNextHook,nCode,wParam,lParam);
end;

procedure Hook;stdcall;
begin
hNextHook := SetWindowsHookEx(WH_CALLWNDPROC,@CallWndProc,HInstance,0);
end;

procedure UnHook;stdcall;
begin
if hNextHook <>0 then
UnhookWindowsHookEx(hNextHook);
end;

procedure InitDll(P:Pointer);stdcall;
begin
IniProc :=P;
end;

exports
Hook,UnHook,InitDll;

begin
hNextHook :=0;
IniProc :=nil;
end.
----------------END OF DLL


---------------------------------------------PROJECT
unit Unit2;

interface

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

function NewWndProc(AHwnd: HWND; uMsg: UINT; wParam: WPARAM; lParam:
LPARAM): UINT; stdcall;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function NewWndProc(AHwnd: HWND; uMsg: UINT; wParam: WPARAM; lParam:
LPARAM): UINT; stdcall;
var
h: hwnd;
begin
try
h := FindWindow('Notepad', nil);
if AHwnd = H then
if (uMsg = WM_SYSCOMMAND) then
if (wParam = SC_MAXIMIZE) then
Form1.Caption := 'Notepad Max';
except
end;
end;

procedure InitDll(P: Pointer); stdcall; external 'project2.dll';

procedure Hook; stdcall; external 'project2.dll';

procedure UnHook; stdcall; external 'project2.dll';

procedure TForm1.FormCreate(Sender: TObject);
begin
InitDll(@NewWndProc);
Hook;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
UnHook;
end;

end.
--------------------------------------------------END OF PROJECT FORM


Peter Below (TeamB)

unread,
May 31, 2008, 1:48:50 PM5/31/08
to
kemal wrote:

> I have 2 projects 1 is exe other is dll
> but my Form1.caption never changes
> where I am wrong ? please HELP

You are trying to do something Windows is designed to prevent: mess
with another processes controls. You can only catch messages for a
control if you manage to subclass the control (the API way), but it
only works if you manage to load a Dll containing the subclassing code
(or a windows hook) into the other process.

If you use a hook DLL you can install the hook from your process, but
you have to be aware that the hook DLL will then get loaded both into
your process *and* into the other process, and these two "instances" of
the DLL share the code, but *not* the data sections. The hNextHook
variable you set in your own process when you call Hook does not carry
its value over to the other process where the hook function is actually
called by the OS. The same applies to your IniProc. And of course you
cannot call a function across process boundaries anyway. This ain't Win
3.1 anymore <g>.

--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com

0 new messages