Entwickler-Ecke

Windows API - DLL einbinden in Hook


FaFu - Do 09.10.08 14:27
Titel: DLL einbinden in Hook
Hallo,
ich hab mir paar Tutorials durchgelesen und hab für die DLL (keyboardhook.dll) folgenden Quelltext:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
library KeyboardHook; 
uses 
  Windows, 
  Messages; 

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

function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; 
begin 

  Result := CallNextHookEx(HookHandle, nCode, wParam, lParam); 
  case nCode < 0 of 
    TRUE: exit; 
    FALSE: 
      begin 

      end; 
  end; 
end; 

function InstallHook(Hwnd: Cardinal): Boolean; stdcall; 
begin 
  Result := False; 
  if HookHandle = 0 then begin 

    HookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, HInstance, 0); 

    WindowHandle := Hwnd; 
    Result := TRUE; 
  end; 
end; 

function UninstallHook: Boolean; stdcall; 
begin 

  Result := UnhookWindowsHookEx(HookHandle); 
  HookHandle := 0; 
end; 

exports 

  InstallHook, 
  UninstallHook; 
end.






Und für mein Projekt wollte ich erstmal nur testen ob es auf einen Tastendruck reagiert.

Hier der Quelltext:



Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
unit Unit1; 

interface 

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

type 
  TInstallHook = function(Hwnd: THandle): Boolean; stdcall; 
  TUninstallHook = function: Boolean; stdcall; 
  TForm1 = class(TForm) 
    procedure FormCreate(Sender: TObject); 
    procedure FormDestroy(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 

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

implementation 

{$R *.dfm} 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
  lib := LoadLibrary('keyboardhook.dll'); 
  if lib <> INVALID_HANDLE_VALUE then 
  begin 
    InstallHook := GetProcAddress(lib, 'InstallHook'); 
    
  end; // else ERROR 
end; 
procedure HookProc(var Result: WParam); 
begin 
if Result = $10000 then 
RenameFile('C:\MyWorks\x.txt','C:\MyWorks\y.txt');         // Test um eine Datei umzunennen
end; 
procedure TForm1.FormDestroy(Sender: TObject); 
begin 
UnInstallHook := GetProcAddress(lib, 'UninstallHook'); 
end; 

end.



$10000 ist ja eigentlich Die Pfeiltaste nach unten und RenameFile war halt nur als Test gedacht.

Was hab ich falsch gemacht? Ich kann alles starten, aber das Prog. tut halt nix

Kann mir jemand helfen, bitte so dass ich es versteh, bin noch ziehmlicher Anfänger auf dem Gebiet.

Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt


bjd - So 12.10.08 17:14

Vielleicht solltest du die Funktionen auch noch aufrufen und nicht nur den DLL-Funktionen zuweisen.


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
InstallHook := GetProcAddress(lib, 'InstallHook');
InstallHook(...);

[...]

UnInstallHook := GetProcAddress(lib, 'UninstallHook');
UnInstallHook(...);


Außerdem solltest du die DLL noch freigeben: FreeLibrary