library Project2;
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
var
hook:cardinal; hwnd:cardinal; hopenfilemap:cardinal=0; p:pointer; switch:boolean; hcreatefilemap:cardinal=0;
oldproc:pointer;
{$R *.res}
function GetMSGProc(ncode:integer;wparam:wparam;lparam:lparam):lresult;
stdcall;
var help:integer;
begin
if ncode<0
then exit;
if (hopenfilemap<>0)
then
begin //wenn handle gültig,dann switch aus MMF auslesen
p:=mapviewoffile(hopenfilemap,FILE_MAP_ALL_ACCESS,0,0,sizeof(boolean));
if p<>
nil then
begin
switch:=boolean(p^);
UnmapViewoffile(p);
end;
end;
if (MSG(Pointer(lparam)^).
message<>WM_KEYDOWN)
then //wenn die Message nicht Keydown
begin //Aufruf von Callnexthook allerdings bei KEYUP und
if (switch)
and (MSG(Pointer(lparam)^).message=WM_KEYUP)
then //switch=true, wparam
MSG(Pointer(lparam)^).wparam:=0;
//auf 0 setzen
result:=Callnexthookex(hook, ncode, wparam, lparam);
exit;
end;
if MSG(Pointer(lparam)^).wparam=107
then
begin //wenn + gedrückt, dann switch verändern, auch in MMF
switch:=not switch;
if (hopenfilemap<>0)
then
begin
p:=mapviewoffile(hopenfilemap,FILE_MAP_ALL_ACCESS,0,0,sizeof(boolean));
if p<>
nil then
begin
boolean(p^):=switch;
UnmapViewoffile(p);
end;
end;
end;
hwnd:=findwindow(
nil,'
form1');
if hwnd<>0
then postmessage(hwnd,WM_USER+1,integer(switch),0);
//message senden
if switch
then MSG(Pointer(lparam)^).wparam:=0;
//wenn switch true dann 0 für wparam
result:=Callnexthookex(hook, ncode, wparam, lparam);
//weitergeben
end;
function hookinstall(h:cardinal):boolean;
stdcall;
begin
hwnd:=H;
result:=false;
switch:=false;
//Installieren des Hooks und Erstellen der MMF
hcreatefilemap:=createfilemapping($FFFFFFFF,
nil,Page_READWRITE,0,sizeof(boolean),'
Switch');
p:=mapviewoffile(hcreatefilemap,File_MAP_ALL_ACCESS,0,0,sizeof(boolean));
if p<>
nil then boolean(p^):=switch;
unmapviewoffile(p);
hook:=setwindowshookex(WH_GETMESSAGE,@GetMSGProc,hinstance,0);
if hook<>0
then result:=true;
end;
function hookuninstall:boolean;
stdcall;
begin
result:=false;
unhookwindowshookex(hook);
closehandle(hcreatefilemap);
//MMF-Handle schließen
result:=true;
end;
procedure DLLMain(Reason:integer);
begin
case reason
of
DLL_PROCESS_ATTACH:
begin //MMF laden, wenn Handle gültig und switch auslesen
hopenfilemap:=openfilemapping(File_MAP_ALL_ACCESS,false,'
Switch');
if (hopenfilemap<>0)
then
begin
p:=mapviewoffile(hopenfilemap,FILE_MAP_ALL_ACCESS,0,0,sizeof(boolean));
if p<>
nil then
begin
switch:=boolean(p^);
UnmapViewoffile(p);
end;
end;
if oldproc<>
nil then
begin
DLLProc:=oldproc;
//alte DLL-Routine laden, wenn sie existiert
DLLproc(reason);
DLLproc:=@DLLMain;
end;
end;
DLL_PROCESS_DETACH:
begin
closehandle(hopenfilemap);
//Handle der MMF schließen
DLLProc:=oldproc;
end;
DLL_Thread_attach: ;
DLL_Thread_Detach:;
end;
end;
exports
hookinstall,
hookuninstall;
begin
oldproc:=@dllproc;
//DLL_Routine durch meine ersetzen
DLLProc:=@DLLMain;
DLLMain(DLL_PROCESS_ATTACH);
end.