global.pas

来自「hook project delphi 5.0 made」· PAS 代码 · 共 71 行

PAS
71
字号
unit Global;

interface

uses windows,Local;

//define the structure of the SetHookHandle function in hookdll.dll
type TSetHookHandle = procedure(HookHandle: HHook); stdcall;

var LibLoaded: boolean; //true if hookdll.dll is already loaded
    LibHandle: HInst;   //dll handle
    HookProcAdd: pointer;  //memory address of hook procedure in windows
    GHookInstalled: boolean;
    SetHookHandle: TSetHookHandle;

function LoadHookProc: boolean;
function SetupGlobalHook: boolean;

implementation

{
LoadHookProc
------------
This function loads the hook procedure from the dll created in hookdll.dll
and obtains a handle for the dll and the address of the procedure in the
dll. The procedure will be called 'GlobalKeyBoardHook'
This procedure also loads the SetHookHandle procedure in hookdll.dll. As
explained in the dll code, this procedure is simply used to inform the dll
of the handle for the current hook, which is needed to call CallNextHookEx
and also to initialise the keyarray (see the dll code).
}
function LoadHookProc: boolean;
begin
    //attempt to load the dll containing our hook proc
    LibHandle:=LoadLibrary('hookdll.dll');
    if LibHandle=0 then begin  //if loading fails, exit and return false
       LoadHookProc:=false;
       exit;
    end;
    //once the dll is loaded, get the address in the dll of our hook proc
    HookProcAdd:=GetProcAddress(LibHandle,'GlobalKeyBoardHook');
    @SetHookHandle:=GetProcAddress(LibHandle,'SetHookHandle');
    if (HookProcAdd=nil)or(@SetHookHandle=nil) then begin //if loading fails, unload library, exit and return false
       FreeLibrary(LibHandle);
       LoadHookProc:=false;
       exit;
    end;
    LoadHookProc:=true;
end;

{
SetupGlobalHook
---------------
This function installs a global hook. To the install a global hook, we first have
to load the hook procedure that we have written from hookdll.dll, using the LoadHookProc
above. If succesful use the setwindowshookex function specifying the hook type as WH_KEYBOARD,
the address of the hook procedure is that loaded from hookdll.dll, the hMod is the handle
of the loaded hookdll.dll and the threadid is set to 0 to indicate a global hook.
}
function SetupGlobalHook: boolean;
begin
     SetupGlobalHook:=false;
     if LibLoaded=false then LibLoaded:=LoadHookProc; //if hookdll isnt loaded, load it
     if LibLoaded=false then exit;                    //if dll loading fails, exit
     CurrentHook:=setwindowshookex(WH_KEYBOARD,HookProcAdd,LibHandle,0); //install hook
     SetHookHandle(CurrentHook);
     if CurrentHook<>0  then SetupGlobalHook:=true; //return true if it worked
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?