Si se puede inyectar la dll y funcionará. El hook sólo funciona en el contexto del thread que hace la llamada a SetWindowsHookEx. El problema es que si inyectas con CreateRemoteThread para provocar un LoarLibrary en un proceso remoto, el thread creado termina tras la inyección y por eso dejará de funcionar el hook al mouse.
La solución es crear un nuevo Thread que se mantenga en el proceso inyectado y listo:
Código Delphi
[-]
library hook;
uses
Windows,
Messages,
sysutils,
dialogs;
const
WH_MOUSE_LL = 14;
var
HookHandle: Cardinal;
TID: DWORD;
{$R *.res}
function StopMouseHook: Boolean;stdcall;
begin
UnhookWindowsHookEx(HookHandle);
if HookHandle = 0 then
Result := False
else
Result := True;
end;
function LowLevelMouseProc(nCode: Integer; wParam: wParam; lParam: lParam): LRESULT; stdcall;
Type
tagMSLLHOOKSTRUCT = record
POINT: TPoint;
mouseData: DWORD;
flags: DWORD;
time: DWORD;
dwExtraInfo: DWORD;
end;
TMSLLHOOKSTRUCT = tagMSLLHOOKSTRUCT;
PMSLLHOOKSTRUCT = ^TMSLLHOOKSTRUCT;
var
Delta:Smallint;
posx:Integer;
posy:integer;
begin
if (nCode >= 0) then
begin
if wParam = WM_LButtonDOWN then
begin
Windows.Beep(1000, 100);
end;
end;
Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
end;
function startMouseHook: Boolean;stdcall;
var
msg:TMsg;
begin
Result := false;
if HookHandle = 0 then
begin
HookHandle := SetWindowsHookEx(WH_MOUSE_LL, @LowLevelMouseProc, Hinstance, 0);
Result := HookHandle <> 0;
end;
end;
procedure ThreadHook;
var
msg: TMsg;
begin
startMouseHook;
repeat
PeekMessage(msg,0,0,0, PM_REMOVE);
until (msg.message = WM_QUIT);
StopMouseHook;
end;
procedure DLLEntryPoint(dwReason: integer);
begin
case dwReason of
DLL_PROCESS_ATTACH:
begin
CloseHandle(CreateThread(nil, 0, @ThreadHook, nil, 0, TID));
end;
DLL_PROCESS_DETACH:
begin
PostThreadMessage(TID, WM_QUIT, 0, 0);
end;
end;
end;
begin
DllProc:=@DLLEntryPoint;
DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
Este problema no aparece si realizas la técnica de inyección por suspensión de thread, más compleja y que requiere inyección directa.
Ten en cuenta que si tu S.O. es de 64bits, no puedes inyectar una dll 32bits en procesos 64bits. Para eso debes compilar tu dll y tu inyector a 64bits.
Saludos.