安装函数:
procedure TfrmTest.btnInstallClick(Sender: TObject);
type
TInstallFunc=function (appName:string):Boolean;stdcall;
var
strPro:string;
pg_h : THandle;
pg_id: DWORD;
InstallFunc:TInstallFunc;
begin
//
strPro :=' 请输入程序名称 !';
if edtProgramName.Text='' then
begin
mmoInfo.Lines.Add(strPro);
end
else
begin
//
mmoInfo.Lines.Add(edtProgramName.Text);
lib_h := LoadLibrary(lib_name);
if lib_h<>0 then
begin
mmoInfo.Lines.Add('load library') ;
InstallFunc := GetProcAddress(lib_h,install_name);
InstallFunc(edtProgramName.Text);
end;
end;
end;
procedure TfrmTest.btnUnInstallClick(Sender: TObject);
type
TUnInstallFunc=procedure()stdcall;
var
UnInstallFunc:TUnInstallFunc;
begin
//
if lib_h<>0 then
begin
//
UnInstallFunc := GetProcAddress(lib_h,uninstall_name);
UnInstallFunc;
mmoInfo.Lines.Add('uninstall hook');
end;
end;
procedure TfrmTest.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if lib_h<>0 then
begin
FreeLibrary(lib_h);
end;
end;
procedure TfrmTest.FormCreate(Sender: TObject);
begin
mmoInfo.Clear;
end;
DLL文件单元,实现键盘钩子函数挂载,并显示窗体
var
keyhhk: THandle;
function KeyProc(iCode:Integer; wp:WPARAM; lp: LPARAM): DWORD; stdcall;
begin
if iCode = HC_ACTION then
begin
if (wp = VK_HOME) and ((1 shr 31) and lp = 0) then
begin
if frmTest = nil then
begin
MessageBox(0, PChar('in...homeproc'), PChar('test'), MB_OK);
frmTest := TfrmTest.Create(nil);
frmTest.Show;
end
else
begin
frmTest.Visible := not frmTest.Visible;
end;
end;
end;
KeyProc := CallNextHookEx(keyhhk, iCode, wp, lp);
end;
function install(appName: string): Boolean; stdcall;
var
app_h: THandle;
app_id: DWORD;
moddll_h: HMODULE;
begin
//
MessageBox(0, PChar('in...install'), PChar('test'), MB_OK);
app_h := FindWindow(nil, PChar(appName));
if app_h <> 0 then
begin
app_id := GetWindowThreadProcessId(app_h);
moddll_h := GetModuleHandle(PChar('InsertDll.dll'));
keyhhk := SetWindowsHookEx(WH_KEYBOARD, @KeyProc, moddll_h, app_id);
end;
end;
procedure uninstall()stdcall;
begin
MessageBox(0, PChar('in...uninstall'), PChar('test'), MB_OK);
UnhookWindowsHookEx(keyhhk);
end;
procedure DllEnterProc(reason: Integer);
begin
case reason of
Windows.DLL_PROCESS_ATTACH:
begin
end;
Windows.DLL_PROCESS_DETACH:
begin
frmTest.Free;
frmTest := nil;
end;
end;
end;
exports
install,
uninstall;
begin
DllProc := @DllEnterProc;
end.