Autor Beitrag
Fabian E.
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 554

Windows 7 Ultimate
Visual Studio 2008 Pro, Visual Studion 2010 Ultimate
BeitragVerfasst: Mi 09.04.08 20:30 
Hallo zusammen,

habe ein Problem. ich habe eine kleinen hook geschrieben, der wichtige systemkombinationen blockt.
dieser klappt auch soweit ganz gut. allerdings lässt er machmal die explorer.exe abstürzen... ich habe keine ahnung warum und kenne mich leider auch nicht so gut damit aus...
könnte es daran liegen, dass ich manche kombinationen doppelt abfange? einmal mit WH_GetMessage plus WM_HOTKEY und noch einmal mit einen LowLevel Hook?

Hier der code:
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:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
library Hook;

uses
  Windows,
  Messages,
  SysUtils,
  Dialogs;

{$R *.res}

type
  { Structure used by WH_KEYBOARD_LL }
  KBDLLHOOKSTRUCT = record
    vkCode: DWORD;
    scanCode: DWORD;
    flags: DWORD;
    time: DWORD;
    dwExtraInfo: DWORD;
  end;
  PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;

const
  WH_KEYBOARD_LL              = 13;
  LLKHF_ALTDOWN               = $00000020;

var
  // Handle des Hooks
  SysKeyHook                  : Cardinal = 0;
  HotKeyHook                  : Cardinal = 0;

function HookSysKey(nCode: Integer; wParam: Cardinal; lParam: PMSG): LRESULT; stdcall;
var
  pkh                         : PKBDLLHOOKSTRUCT;
  bCtrlKeyDown                : BOOL;
begin
  pkh := PKBDLLHOOKSTRUCT(lParam);

  if nCode = HC_ACTION then
    begin
      bCtrlKeyDown := GetAsyncKeyState(VK_CONTROL) shr ((sizeof(SHORT) * 8) - 1) <> 0;
      // Ctrl+Esc
      if ((pkh^.vkCode = VK_ESCAPE) and bCtrlKeyDown) or
        // Alt+TAB
      ((pkh^.vkCode = VK_TAB) and (pkh^.flags and LLKHF_ALTDOWN = LLKHF_ALTDOWN)) or
        // Alt+Esc
      ((pkh^.vkCode = VK_ESCAPE) and (pkh^.flags and LLKHF_ALTDOWN = LLKHF_ALTDOWN)) or
        // Start Menu
      ((pkh^.vkCode = VK_LWIN) or (pkh^.vkCode = VK_RWIN)) then
        begin
          result := 1;
          exit;
        end;
    end;
  result := CallNextHookEx(SysKeyHook, nCode, wParam, Cardinal(lParam));
end;

function HookHotKey(nCode: Integer; wParam: Cardinal; lParam: PMSG): LRESULT; stdcall;
begin
  if (nCode >= HC_ACTION)
    and (lParam^.message = WM_HOTKEY) then
    begin
      if (lParam^.wParam = 500or
        (lParam^.wParam = 510or
        (lParam^.wParam = 49203then
        begin
          Result := 1;
          Exit;
        end
      else
        ShowMessage(IntToStr(lParam^.wParam));
    end;
  result := CallNextHookEx(HotKeyHook, nCode, wParam, Cardinal(lParam));
end;

function InstallHook: Boolean;
var
  SysKey, HotKey              : Boolean;
begin
  // Wenn Hook bereits installiert ist, abbrechen
  SysKey := SysKeyHook = 0;
  if SysKey then
    begin
      // Hook installieren
      SysKeyHook := SetWindowsHookEx(WH_KEYBOARD_LL, @HookSysKey, hInstance, 0);
      // Zurückgeben, ob Aktion erfolgreich war
      SysKey := SysKeyHook <> 0;
    end;

  HotKey := HotKeyHook = 0;
  if HotKey then
    begin
      // Hook installieren
      HotKeyHook := SetWindowsHookEx(WH_GETMESSAGE, @HookHotKey, hInstance, 0);
      // Zurückgeben, ob Aktion erfolgreich war
      SysKey := SysKeyHook <> 0;
    end;

  Result := SysKey and Hotkey;
end;

function UnInstallHook: Boolean;
var
  SysKey, HotKey              : Boolean;
begin
  // Hook deinstallieren und Handle nullen
  SysKey := UnhookWindowsHookEx(SysKeyHook);
  SysKeyHook := 0;

  HotKey := UnhookWindowsHookEx(HotKeyHook);
  HotKeyHook := 0;

  Result := SysKey and Hotkey;
end;

exports InstallHook,
  UnInstallHook;

begin
end.


Vielleicht weiß ja jemand rat :)

gruß
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 10.04.08 08:32 
Sagt Dir der Explorer eine Absturz-Adresse an? Wenn ja: Wo liegt diese? Innerhalb deiner Hooks-DLL?

Compilier mal mit Mapfile, les mit dem ProcessExplorer das DLL-Offset deiner DLL aus und schau Dir dann mal an, was OmMAP dazu sagt.

Ferner ein kleiner Hinweis: Die Shift States, z.B. für die VK_CONTROL solltest Du von Windows in der Message mit übergeben bekommen (IIRC).

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.