I create a logging system inside the dll, the main task of this is to trap some specific window messages of the target application and perform some tasks. But, unfortunately, only one message gets trapped by the WndProc( WM_GETMINMAXINFO) method . What am I missing? Why aren't all Windows messages logged?
This is the minimum sample code to demonstrate the problem.
Dll
library LogDll;
uses
Winapi.Windows,
Winapi.Messages,
System.IOUtils,
System.SysUtils,
System.Classes;
{$R *.res}
type
TLogInspector = class
private
WndHandle: THandle;
ProcAddrInst: Pointer;
OrgWndProc: Pointer;
protected
function CallOrgWndProc(Message: TMessage): LRESULT;
procedure WndProc(var Message: TMessage); virtual;
public
constructor Create(AHandle: THandle); virtual;
end;
var
MainHook: HHook;
Log : TLogInspector;
function TLogInspector.CallOrgWndProc(Message: TMessage): LRESULT;
begin
Result := CallWindowProc(OrgWndProc, WndHandle, Message.Msg, Message.wParam, Message.lParam);
end;
constructor TLogInspector.Create(AHandle: THandle);
begin
OrgWndProc := Pointer(GetWindowLongPtr(AHandle, GWL_WNDPROC));
ProcAddrInst := MakeObjectInstance(WndProc);
WndHandle := AHandle;
SetWindowLongPtr(WndHandle, GWL_WNDPROC, LONG_PTR(ProcAddrInst));
end;
procedure TLogInspector.WndProc(var Message: TMessage);
begin
//log the current message
TFile.AppendAllText('C:\Delphi\log.txt', 'WndProc '+IntToStr(Message.Msg)+sLineBreak);
//call the org WndProc
Message.Result := CallOrgWndProc(Message);
end;
function HookCallBack(nCode: Integer; _WPARAM: WPARAM; _LPARAM: LPARAM): LRESULT; stdcall;
var
lpClassName : array [0 .. 256] of Char;
begin
if nCode = HCBT_CREATEWND then
begin
GetClassName(_WPARAM, lpClassName, 256);
if lpClassName = 'TForm1' then
Log:= TLogInspector.Create(_WPARAM);
end;
Result := CallNextHookEx(MainHook, nCode, _WPARAM, _LPARAM);
end;
procedure InitLog; stdcall;
begin
MainHook := SetWindowsHookEx(WH_CBT, @HookCallBack, 0, GetCurrentThreadId);
end;
procedure DoneLog; stdcall;
begin
UnhookWindowsHookEx(MainHook);
end;
exports
InitLog, DoneLog;
begin
end.
Application
type
TForm1 = class(TForm)
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure InitLog; stdcall; external 'LogDll.dll' name 'InitLog';
procedure DoneLog; stdcall; external 'LogDll.dll' name 'DoneLog';
implementation
{$R *.dfm}
initialization
InitLog;
finalization
DoneLog;
end.
source
share