Einzelnen Beitrag anzeigen

Chaosente

Registriert seit: 20. Aug 2006
80 Beiträge
 
#10

Re: Hook.dll und programm liefern kein ergebnis

  Alt 6. Sep 2007, 22:27
ja klar kein problem, also hier mal der code der unit...
Delphi-Quellcode:
unit Unit1;

interface

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

const
  WM_KeyLogMessage = WM_USER + 23;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure OnKeyLogMessage(var Msg:Tmessage); message WM_KeyLogMessage;
  public
    { Public-Deklarationen }
  end;


  TInstallHook = function(Hwnd: THandle): Boolean; stdcall;
  TUninstallHook = function: Boolean; stdcall;

var
  Form1: TForm1;
  InstallHook: TInstallHook;
  UninstallHook: TUninstallHook;
  lib: Cardinal;

implementation

{$R *.dfm}

procedure TForm1.OnKeyLogMessage(var msg:Tmessage);
begin
memo1.Lines.Add(chr(msg.wparam));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
lib := LoadLibrary('keyboardhook.dll');
  if lib <> INVALID_HANDLE_VALUE then begin
    InstallHook := GetProcAddress(lib, 'InstallHook');
    UnInstallHook := GetProcAddress(lib, 'UninstallHook');
  end; // else ERROR
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
freelibrary(lib);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
installhook(self.Handle);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
uninstallhook;
end;

end.
.dll
Delphi-Quellcode:
library Keyboardhook;

uses
  Windows,
  Messages;

const
  WM_KeyLogMessage = WM_USER + 23;

var
  HookHandle: Cardinal = 0;
  WindowHandle: Cardinal = 0;


function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM):
 LRESULT; stdcall;
begin
//es ist ebenfalls möglich die Bearbeitung an eine Bedingung zu knüpfen
//it's possible to call CallNextHookEx conditional only.
  Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
  case nCode < 0 of
    TRUE: exit; //wenn code kleiner 0 wird nix gemacht
                //if code smaller 0 nothing has to be done
    FALSE:
      begin
       sendmessage(WindowHandle, WM_KeyLogMessage,wparam,lparam);
//Hier kann jetzt alles bearbeitet werden
//Here one can work with the parameters
      end;
  end;
end;

function InstallHook(Hwnd: Cardinal): Boolean; stdcall;
begin
  Result := False;
  if HookHandle = 0 then begin
//Erstmal Hook installieren
//First install the hook
    HookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc,
    HInstance, 0);
//Uebergebenes Fensterhandle sichern
//Save the given window handle
    WindowHandle := Hwnd;
    Result := TRUE;
  end;
end;

function UninstallHook: Boolean; stdcall;
begin
//Hook aus der Hookchain entfernen
//Uninstall hook from hook chain
  Result := UnhookWindowsHookEx(HookHandle);
  HookHandle := 0;
end;

exports
//Installations- und Deinstallationsroutine exportieren
//Export the installation and deinstallation routine
  InstallHook,
  UninstallHook;
end.
geht das so? oder wie meintest du das mit der demo? mitlerweile kommen öfter auch mal noch mehr buchstaben an.... ganz merkwürdig
vllt findest du ja was
  Mit Zitat antworten Zitat