键盘hook,dll实现挂载到其他程序

安装函数:
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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蚂蚁_CrkRes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值