DLL代码如下:
-------------
library Inject;{ 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,
  UnitHook in 'UnitHook.pas';{$R *.res}
exports
    StartHook,StopHook;begin
end.UnitHook代码如下:
------------------
unit UnitHook;interfaceuses Windows, SysUtils, Classes, math, messages, dialogs;const
  WM_TestMessage = WM_USER + 2004;
  WM_IATMessage = WM_USER + 2005;  TrapMode = False; //True陷阱式,False表示改引入表式     function StartHook(mHandle : longint;QQProcID : Cardinal):boolean;stdcall;export;
     function StopHook():boolean;stdcall;export;implementationvar
   MainHandle : longint;
   MessageHook : HHOOK;function MyHookProc(iCode:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT; stdcall;
begin
     if iCode < 0 then
     begin
          Result := CallNextHookEx(MessageHook, iCode, wParam, longint(@lParam));
          exit;
     end;
     if iCode = HC_ACTION then
     begin
        if PMsg(lParam)^.Message = WM_TestMessage then
        begin
             showmessage('Recv This Msg');
        end;
     end;
     Result := CallNextHookEx(MessageHook, iCode, wParam, longint(@lParam));
end;function StartHook(mHandle : longint;QQProcID : Cardinal):boolean;stdcall;export;
begin
     MainHandle := mHandle;
     showmessage(IntToStr(MainHandle) + '  -  ' + IntToStr(QQProcID));
     Result := false;
     if MessageHook = 0 then
     begin
          MessageHook := SetWindowsHookEx(WH_GETMESSAGE,MyHookProc,hInstance,0);
          if longint(MessageHook) <> 0 then
             Result := True
          else
              showmessage('Hook Error');
     end;
end;function StopHook():boolean;stdcall;export;
begin
     if MessageHook <> 0 then
     begin
          UnHookWindowsHookEx(MessageHook);
          MessageHook := 0;
     end;
     Result := True;
end;end.调用StartHook函数时,showmessage显示的值和传入的值不等,不知何故。请高手指点。

解决方案 »

  1.   

    看起来没有任何问题,不知道楼主是怎么调的 StartHook 函数?有人批评我:不要说一些别人未问的问题,不要自作聪明。可我还是要提醒楼主一下这段代码有 bug :
    请注意 MessageHook 所在的进程空间。楼主是想获得 qq密码框里的密码,呵呵,我也做过啊,是练习使用钩子的好途径。
      

  2.   

    呵呵!问题已经解决,是我在EXE中申明2个导出的函数时,没写stdcall。但现在还有一个问题,DLL里面没有全局变量的吗?
    为什么在StartHook中显示正确了,MainHandle到了别的函数里又变成0了呢?