unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, TLHelp32;
type
TForm1 =
class(TForm)
btnHook: TButton;
edWndToHook: TEdit;
lvMain: TListView;
procedure btnHookClick(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
protected
procedure _WMCopyData(
var Msg: TWMCopyData);
message WM_COPYDATA;
public
{ Public declarations }
end;
var
Form1: TForm1;
WindowHandle: THandle;
function InstallHook(DWThreadID: DWORD): boolean;
stdcall external '
HookDLL.dll';
procedure UnInstallHook;
stdcall external '
HookDLL.dll'
implementation
{$R *.dfm}
function MyEnumWindowProc(Wnd: THandle; LParam: LongWord): boolean;
stdcall;
var
WinCaption :
string;
Len: integer;
begin
Result := True;
Len := GetWindowTextLength(Wnd);
SetLength(WinCaption, Len);
GetWindowText(Wnd, PChar(WinCaption), Len+1);
if (Copy(WinCaption, 0, 7) = '
TextPad')
then
begin
WindowHandle := Wnd;
end;
end;
function GetWindowHandleByExeName(
const AExeName:
string): THandle;
var
SnapShot: THandle;
p: TProcessEntry32;
ProcessHandle: THandle;
begin
Result := 0;
WindowHandle := 0;
ProcessHandle := 0;
p.dwSize := SizeOf(p);
SnapShot := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0);
try
if Process32First(SnapShot, p)
then
repeat
if AnsiLowerCase(AExeName) = AnsiLowerCase(p.szExeFile)
then
ProcessHandle := p.th32ProcessID;
until (ProcessHandle <> 0)
or not Process32Next(SnapShot, p);
EnumWindows(@MyEnumWindowProc, ProcessHandle);
Result := WindowHandle;
finally
CloseHandle(SnapShot);
end;
end;
procedure TForm1.btnHookClick(Sender: TObject);
begin
// GetWindowThreadProcessId(GetWindowHandleByExeName('Project1.exe'), nil)
if not InstallHook(0)
then
raise Exception.Create('
Error installing hook');
end;
procedure TForm1._WMCopyData(
var Msg: TWMCopyData);
var
aMsg: ^tagMsg;
WinCaption :
string;
Wnd : THandle;
begin
aMsg := Msg.CopyDataStruct.lpData;
Wnd := aMsg^.HWND;
SetLength(WinCaption, GetWindowTextLength(Wnd));
GetWindowText(Wnd, PChar(WinCaption), GetWindowTextLength(Wnd)+1);
if (Copy(WinCaption, 1, 7) = '
TextPad')
then
begin
with lvMain.Items.Add
do
begin
Caption := IntToStr(aMsg^.time);
SubItems.Add(WinCaption);
SubItems.Add(IntToStr(aMsg^.
message));
SubItems.Add(IntToStr(aMsg^.wParam));
SubItems.Add(IntToStr(aMsg^.lParam));
end;
end;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
UninstallHook;
end;
end.