Autor Beitrag
MCQ
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 75

Windows 95/98/XP
Delphi 7 Etp.
BeitragVerfasst: Sa 22.10.05 17:21 
Ich wollte mich heute mal mit dem Thema Hooks auseinandersetzen. Ich fand auch einige Tutorials dazu. Laut diesen sollte folgender Code bei jedem klick mit der linken Maustaste eine Message ausgeben. Es tut sich allerdings gar nichts. Ich hab auch mal einen Brakepoint auf die Funktion "UnsereMouseFunktion" gesetzt. Aufgerufen wird sie nicht :(

ausblenden volle Höhe 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:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  hMouseHook:HHOOK;

implementation

{$R *.dfm}

function UnsereMouseFunktion(nCode: integer; wparam: WPARAM; lParam: LPARAM):Integer; stdcall;
begin
 if(wParam = WM_LBUTTONDOWN) then ShowMessage('Linke Maustaste gedrückt!');
 Result := CallNextHookEx(hMouseHook, nCode, wParam, lParam);
end;

procedure TForm1.FormCreate(Sender:TObject);
begin
 hMouseHook := SetWindowsHookEx( WH_MOUSE  , UnsereMouseFunktion, 00);

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
UnhookWindowsHookEx(hMouseHook);
end;

end.


Ich bin für jegliche Hilfe dankbar

Gruß
MCQ


Zuletzt bearbeitet von MCQ am Sa 22.10.05 17:31, insgesamt 2-mal bearbeitet
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 22.10.05 17:22 
Hallo,

bitte ändere den Titel des Topics, da er wenig über das eigentlich Thema verrät. Hier der entsprechende Absatz aus den Richtlinien:

1.2 Beiträge:
Bitte formuliere den Betreff Deiner Beiträge so, dass andere Mitglieder anhand dieser bereits das eigentliche Thema festmachen können. Beiträge wie etwa "Eine Anfängerfrage" oder "Weiß jemand, wie das geht?" lassen den Leser im Unklaren darüber, was das Thema der Diskussion ist.[...]


Einfach oben bei Deinem ersten Beitrag auf user defined image klicken und den Titel ändern. Dank Dir!

Viele Grüße,
Christian S.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
MCQ Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 75

Windows 95/98/XP
Delphi 7 Etp.
BeitragVerfasst: Sa 22.10.05 17:31 
Sorry wenn der Name des Themas immernoch nicht besser ist, aber was besseres viel mir nun wirklich nicht ein. Wüsste ich wo genau das Problem liegt müsste ich wahrscheinlich nicht fragen sondern würde googlen. Ich kann aber nunmal nicht mehr sagen kann als das das Ding nicht läuft.
Raffo
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 300



BeitragVerfasst: Sa 22.10.05 17:41 
habe diesen Thread gefunden, darin auch eine Anleitung zu "Hook", vielleicht willst Du Dich damit auseinandersetzen: www.delphi-forum.de/...1&highlight=hook
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Sa 22.10.05 17:41 
den code musst du in einer dll auslagern, such mal bei google nach "assarbad" und "tastaturhook"

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
MCQ Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 75

Windows 95/98/XP
Delphi 7 Etp.
BeitragVerfasst: Sa 22.10.05 18:16 
Habs in einer DLL ausgelagert. Resultat ist folgendes:

ausblenden volle Höhe 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:
library Hook;

uses
  SysUtils,
  Classes,
  Windows,
  Messages,
  Dialogs;


var HookHandle:Cardinal=0;
{$R *.res}


function UnsereMouseFunktion(nCode: integer; wparam: WPARAM; lParam: LPARAM):Integer; stdcall;
begin
  if(wParam = WM_LBUTTONDOWN) then ShowMessage('Linke Maustaste gedrückt!');
  Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
end;

Function StartHook():boolean;
begin
  Result:=false;
  if HookHandle <> 0 then exit;
  HookHandle:=SetWindowsHookEx( WH_GETMESSAGE  , @UnsereMouseFunktion, 00);
  Result:=true;
end;

Function EndHook():boolean;
begin
  Result:=false;
  if HookHandle = 0 then exit;
  UnhookWindowsHookEx(HookHandle);
  HookHandle:=0;
end;

exports
  StartHook,
  EndHook;



end.


Problem bei der Sache. Es funktioniert genausowenig. Die Procedure StartHook liefert das Resultat true, aber selbst wenn ich eine MessageBox in UnsereMouseFunktion implementiere die keinerlei if-Anweisung bedarf passiert rein gar nichts.Das Tutorial von assarbad bin ich grad am lesen, scheint aber das selbe drin zu stehen wie in den anderen auch.
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Sa 22.10.05 18:22 
bei assarbad.net/en/stuff/tutorials/hooks/ ist auf jedenfall eine funktionierende lösung

z.b. darfst du wenn nCode > 0 ist glaub ich nicht die CallNextHookEx aufrufen aber das siehste da schon

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
MCQ Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 75

Windows 95/98/XP
Delphi 7 Etp.
BeitragVerfasst: Sa 22.10.05 18:41 
Also jetzt versteh ich nix mehr. Ich habe die StartHook-Funktion mal nen bisschen geändert, so das mir HookHandle ausgegeben wird. HookHandle ist auch nach ausführung der SetWindowsHookEx-Funktion noch 0. Irgendwas mach ich falsch, ich hab nur noch nicht rausgefunden was :(
MCQ Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 75

Windows 95/98/XP
Delphi 7 Etp.
BeitragVerfasst: Sa 22.10.05 18:45 
Hab einfach mal
ausblenden Delphi-Quelltext
1:
HookHandle:=SetWindowsHookEx( WH_MOUSE  , @UnsereMouseFunktion, 00);					

in
ausblenden Delphi-Quelltext
1:
HookHandle:=SetWindowsHookEx( WH_MOUSE  , @UnsereMouseFunktion, HInstance, 0);					

geändert und schon funktioniert es offenbar. Allerdings scheint der Hook nun nur ein Lokaler zu sein, ich wollte aber einen globalen schreiben :(
MCQ Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 75

Windows 95/98/XP
Delphi 7 Etp.
BeitragVerfasst: Sa 22.10.05 19:26 
Problem gelöst, mein erster Hook läuft *stolz is*