
29.11.2012, 15:52
|
Начинающий
|
|
Регистрация: 13.02.2010
Сообщения: 104
Репутация: 10
|
|
Поздравьте меня) я справился) всем спасибо)
вот Длл
Код:
library hooklib;
uses
Windows,
Messages;
const
HookMap = '{F3E25943-FCC7-43E5-BE22-7CF35EA5FCC6}';
type
PHookData = ^THookData;
THookData = packed record
AppWnd : HWND;
OldHook : HHOOK;
end;
var
hMap : THandle = 0;
HookData : PHookData = nil;
procedure DLLEntryPoint(dwReason: DWORD);
begin
case dwReason of
DLL_PROCESS_ATTACH:
begin
hMap := CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, SizeOf(THookData), HookMap);
HookData := MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(THookData));
end;
DLL_PROCESS_DETACH:
begin
UnMapViewOfFile(HookData);
CloseHandle(hMap);
end
end
end;
function HookProc(Code: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT;stdcall;
begin
if Code>=0 then
begin
SendMessage(HookData^.AppWnd,Code,WParam,LParam);
Result := 0;
end
else
Result := CallNextHookEx(HookData^.OldHook, Code, WParam, LParam);
end;
function SetHook(Wnd: HWND;dwThreadId: DWORD;WH:Integer): BOOL; stdcall;
begin
if HookData <> nil then
begin
HookData^.AppWnd := Wnd;
HookData^.OldHook := SetWindowsHookEx(WH, HookProc, HInstance,dwThreadId);
Result:=HookData^.OldHook <> 0;
end else
Result:=False;
end;
function RemoveHook: BOOL; stdcall;
begin
Result := UnhookWindowsHookEx(HookData^.OldHook);
end;
exports
SetHook, RemoveHook;
begin
if @DLLProc = nil then DLLProc := @DLLEntryPoint;
DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
а вот вызова
Код:
type
TMes = class
private
FHWnd: HWND;
protected
procedure WMCopyData(var Msg:TMessage);virtual;
public
constructor Create();
destructor Destroy; override;
end;
procedure ProcessMessages;
var
Msg: TMsg;
function ProcessMessage(): Boolean;
var
Handled: Boolean;
begin
Result := False;
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
Result := True;
if Msg.Message <> WM_QUIT then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
end;
begin
while ProcessMessage() do {loop};
end;
constructor TMes.Create();
begin
inherited Create();
FHWnd := AllocateHWnd(WMCopyData);
end;
destructor TMes.Destroy;
begin
DeallocateHWnd(FHWnd);
inherited Destroy;
end;
var
Code_:LongWord;
Handle:longword;
messa:TMes;
function SetHook(Wnd: HWND;dwThreadId:LongWord;WH:Integer): BOOL; stdcall; external 'hooklib.dll';
function RemoveHook: BOOL; stdcall; external 'hooklib.dll';
function StartWait(dwThreadId,code,WH:longword):boolean;
begin
Code_:=code;
messa:=TMes.Create;
result:=SetHook(messa.FHWnd,dwThreadId,WH);
end;
function WaitMessages(dwMilliseconds: LongWord):longword;
var
i:cardinal;
begin
Handle:=0;
i:=0;
repeat
ProcessMessages;
inc(i);
until(Handle>0)or(i=dwMilliseconds);
Result:=Handle;
messa.Destroy;
end;
procedure TMes.WMCopyData(var Msg: TMessage);
begin
if(Msg.Msg=Code_)then
begin
Handle:=Msg.WParam;
RemoveHook;
end;
end;
...
StartWait(ProcInfo.dwThreadId,HCBT_CREATEWND,WH_CBT);
Handle:=WaitMessages(INFINITE);
|