看流星社区

 找回密码
 注册账号
查看: 2757|回复: 4

[Delphi] 注入呼出的一件郁闷事情

[复制链接]

该用户从未签到

发表于 2011-4-2 08:55:11 | 显示全部楼层 |阅读模式
这几天学习钩子注入呼出,参考各位大大的代码,左拼右凑的写了一个注入呼出,是传奇的,开始的时候呼不出,几经查找修改后,变成现在这样,在游戏里按 HOME键时,辅助窗体按着 home键时不停的闪烁,当松开home键后,窗体消失,但按一会儿后,窗体就可以正常显示了,小弟不知道那里出错了,参考别人的代码,他们却可以正常显示,  
现在贴出代码,希望各位帮忙看一下:  
exe 代码:  
unit Unit1;  

interface  

uses  
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
Dialogs, StdCtrls,ExtCtrls;  

type  
TForm1 = class(TForm)  
Button1: TButton;  
Button2: TButton;  
procedure FormClose(Sender: TObject; var Action: TCloseAction);  
procedure Button1Click(Sender: TObject);  
procedure FormCreate(Sender: TObject);  
procedure Button2Click(Sender: TObject);  
private  
{ Private declarations }  
public  
{ Public declarations }  
end;  

var  
Form1: TForm1;  
function HookOn(iphwnd:HWND;iptype:LongInt):LongInt;stdcall;external 'mydll' name 'HookOn';  
function HookOff:Boolean;stdcall;external 'mydll' name 'HookOff';  
implementation  

{$R *.dfm}  





procedure TForm1.FormCreate(Sender: TObject);  
var  
h1:HWND;  
begin  
h1:=FindWindow(nil,'legend of mir2');  
if h1=0 then ShowMessage('没找到游戏');  
if h1>0 then Button1.Caption:='DLL注入';  
Button2.Caption:='取消注入';  
HookOn(h1,WH_KEYBOARD);  
end;  

procedure TForm1.Button1Click(Sender: TObject);  
var  
h1:HWND;  
begin  
if Button1.Caption= 'DLL注入' then  
begin  
Button1.Caption:='按home启动';  
h1:=FindWindow(nil,'legend of mir2');  
Sleep(1000);  
HookOn(h1,WH_KEYBOARD);  
end;  
end;  
procedure TForm1.Button2Click(Sender: TObject);  
begin  
HookOff;  
Button1.Caption:='DLL注入';  
end;  
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);  
begin  
HookOff;  
end;  
end.  

DLL部分:  
library mydll;  

{ Important note about DLL memory management: ShareMem must be the  
first unit in your library's USES clause AND your project's (select  
Project-View Source) USES clause if your DLL exports any procedures or  
functions that pass strings as parameters or function results. This  
applies to all strings passed to and from your DLL--even those that  
are nested in records and classes. ShareMem is the interface unit to  
the BORLNDMM.DLL shared memory manager, which must be deployed along  
with your DLL. To avoid using BORLNDMM.DLL, pass string information  
using PChar or ShortString parameters. }  

uses  
SysUtils,  
Classes,  
Forms,  
dllform in 'dllform.pas' {Form1};  

{$R *.res}  
exports  
hookon,hookoff;  

begin  

end.

该用户从未签到

 楼主| 发表于 2011-4-2 08:55:38 | 显示全部楼层
dll窗体部分:
unit dllform;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RzTabs,StdCtrls;

type
  TForm1 = class(TForm)
    RzPageControl1: TRzPageControl;
    TabSheet1: TRzTabSheet;
    TabSheet2: TRzTabSheet;
    TabSheet3: TRzTabSheet;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  function HookProc(nCode:integer;wparam:WPARAM;lparam:LPARAM):LRESULT;stdcall;
  function HookOn(IpHwnd:HWND;IpType:LongInt):LongInt;stdcall;export;
  function HookOff:Boolean;stdcall;export;


implementation
var
  hHk:HHOOK=0;
  mhwnd:HWND=0;
  bshow:Integer=1;
  hThread:Cardinal;
  hmod:Pointer;
  hprocessid:Cardinal;
  mmode:Integer;

{$R *.dfm}
{建立键盘钩子}
function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall;
begin
//接收按键F8开始挂钩DLL
if (bShow=1) And (wParam=VK_Home) then
    begin
bShow:=5;
Form1:=TForm1.Create(Application);
Form1.Show;
ShowCursor(true);
end;
if (bShow=2) And (wParam=VK_Home) then
    begin
bShow:=6;
Form1.Free;
end;

if (bShow=3) And (wParam=VK_Home) then
    begin
bShow:=2;
end;

if (bShow=4) And (wParam=VK_Home) then
    begin
bShow:=1;
end;
if (bShow=5) then bShow:=3;
if (bShow=6) then bShow:=4;
Result := CallNextHookEx(hHk,nCode,WParam,LParam);
end;


    function HookOn(iphwnd:HWND; ipType:LongInt):LongInt;stdcall;export;
    begin
      hThread:= GetWindowThreadProcessId(iphwnd,hmod);
      hHk:=SetWindowsHookEx(iptype,@hookproc,HInstance,hthread);
      result:=hHk;
    end;
    function HookOff:Boolean;stdcall;export;
    begin
      if hHk<>0 then
      begin
        UnhookWindowsHookEx(hHk);
        hHk:=0;
        Result:=True;

        end else
        Result:=False;
      end;
     
end.

该用户从未签到

发表于 2011-4-2 08:56:00 | 显示全部楼层
function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall;
begin  
if (nCode = HC_ACTION) and ((lParam and $80000000) = 0) then
  begin
case wParam of
VK_HOME: begin

try
            if Form1 = nil then
            begin


              Application.Handle := GetForegroundWindow; //hwnd;
              Form1 := TForm1.Create(Application);

            end;
          finally
            if Assigned(Form1) then Form1.Visible := not Form1.Visible;
          end;
end;
end;
Result := CallNextHookEx(hHk, nCode, WParam, LParam);
end;

该用户从未签到

发表于 2011-4-2 08:56:25 | 显示全部楼层
你的代码我就不看了,头晕,我给你一个我在使用的,一切正常
library HookDll;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  Windows,
  Messages,
  SysUtils,
  Classes,
  Forms,
  Variants,
  uDll in 'uDll.pas' {FrmDll},
  uFunc in 'uFunc.pas',
  uGlobal in 'uGlobal.pas';

{$R *.res}

var
  HK: HHOOK = 0;
  HMod: Pointer;
  ProcID: Cardinal;

//--------------------------勾子子程-----------------------------------
function HookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
label
  ToExit;
begin
  if nCode < 0 then goto ToExit;
  if nCode <> HC_ACTION then goto ToExit;
  if (LPARAM and $80000000) = 0 then goto ToExit;
  if WPARAM=VK_F12 then
  begin
    if FrmDll = nil then FrmDll:=TFrmDll.Create(Application);
    FrmDll.Visible := not FrmDll.Visible;
  end;
  ToExit:
  Result := CallNexthookex(HK,nCode,wparam,lparam);
end;

//--------------------------安装勾子-----------------------------------
function HookOn(AHwd: HWND): LongInt; stdcall; export;
begin
  ProcID := GetWindowThreadProcessId(AHwd,HMod);
  if ProcID <> 0 then HK := SetWindowsHookEx(WH_KEYBOARD,@HookProc,hinstance,ProcID);
  Result := HK;
end;

//--------------------------关闭勾子-----------------------------------
function HookOff(AHHK: HHOOK): Boolean; stdcall; export;
begin
  Result := False;
  if AHHK <> 0 then begin
    UnhookWindowsHookEx(AHHK);
    Result := True;
  end;
end;

procedure MyDllProc(Reason: Integer);
begin
  case Reason of
    DLL_PROCESS_DETACH:
      begin
        FreeAndNil(FrmDll);
        //ShowMessage('整个DLL的善後程序');
      end;
    DLL_Process_Attach:
      begin
        //ShowMessage('整个DLL的初始化代码');
      end;

    DLL_Thread_Attach:
      begin
        //FrmDll:=TFrmDll.Create(Application);
        //ShowMessage('当主叫端开始一个Thread时');
      end;
    DLL_Thread_Detach:
      begin
        //ShowMessage('当主叫端终止一个Thread时');
      end;
  end;
end;

exports HookOn, HookOff;

begin
  //DllProc := @MyDllProc
end.

该用户从未签到

 楼主| 发表于 2011-4-2 08:56:42 | 显示全部楼层
找到原因了,exe代码乱了,改为简单的,现在正常了
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-4-27 00:08

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表