Thema: Delphi keyboard hook / mmf

Einzelnen Beitrag anzeigen

Benutzerbild von cherry
cherry

Registriert seit: 14. Nov 2005
561 Beiträge
 
RAD-Studio 2009 Ent
 
#2

Re: keyboard hook / mmf

  Alt 26. Mär 2009, 09:18
Damit ihr nicht den ganzen karumpel herunterladen müsst, hier doch noch einen Ausschnitt aus meinem Quelltext (So wie er momentan kompilliert ist.)...

Anzufügen ist: Vorhin hat das Programm funktioniert, bis ich ein anderes Fenster (ein Fenster nicht von meinem Programm) aktiviert habe. Wenn ein anderes Programm fokusiert ist, gibts bei jeder Taste die ich drücke eine "Zugriffsverletzung" bei irgend einer Speicheradresse...

an was liegts denn wohl?

hooks dll:

Delphi-Quellcode:
library hooks;

uses
  Windows,
  Messages,
  Dialogs;

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

const
  keybmsg = 'KeyboardHookMessage_Enemyleft';

{$INCLUDE Include\HookingDLL_Data.pas}

function GetPView: Pointer; stdcall;
begin
  Result := lpView;
end;

function CreateMMF: Boolean;
begin
  Result := False;
  MapHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(TDLLData), MMFName);
  if MapHandle <> 0 then
  begin
    if Assigned(lpView) then
      Result := True
    else
      CloseHandle(MapHandle);
  end;
end;

function OpenAndMapMMF: Pointer;
begin
  Result := nil;
  MapHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(TDLLData), MMFName);
  if MapHandle <> 0 then
  try
    lpView := MapViewOfFile(MapHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TDLLData))
  finally
    CloseHandle(MapHandle);
  end;
  Result := lpView;
end;

function CloseAndUnmapMMF(view: Pointer): Boolean;
begin
  result := UnMapViewOfFile(view);
end;

function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
  lpView: PDLLData;
begin
  // it's possible to call CallNextHookEx conditional only.
  Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
  case nCode < 0 of
    TRUE: exit;// if code smaller 0 nothing has to be done
    FALSE:
    begin
      lpView := OpenAndMapMMF;
      if Assigned(lpView) then
      try
        lpView^.nCode := nCode;
        lpView^.ProcessID := GetCurrentProcessID;
        lpView^.ThreadID := GetCurrentThreadID;
        lpView^.TestStr := '#enemyleft';
        // we use HWND_BROADCAST to send the message to any window which handles this WM
        SendMessage(HWND_BROADCAST, lpView^.WM_KEYBHOOKMSG, wParam, lParam);
      finally
        CloseAndUnMapMMF(lpView);
      end;
    end;
  end;
end;

function InstallHook(Hwnd: Cardinal): Boolean; stdcall;
begin
  //lpView^.WM_KEYBHOOKMSG := WM_USER+1;
 // WM_KEYBHOOKMSG := RegisterWindowMessage(WM_USER+13);
  Result := False;
  if HookHandle = 0 then
  begin
    // install the hook
    HookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, HInstance, 0);
    // save the given window handle
    WindowHandle := Hwnd;
    Result := TRUE;
  end;
end;

procedure FreeMMF;
begin
  //UnmapviewofFile(lpView);
  //CloseHandle(MapHandle);
end;

function UninstallHook: Boolean; stdcall;
begin
  //
  FreeMMF;
  // uninstall hook from hook chain
  Result := UnhookWindowsHookEx(HookHandle);
  HookHandle := 0;
end;

procedure DLLMain;
begin
  if CreateMMF then
  begin
    // register our private window messages
    lpView^.WM_KEYBHOOKMSG := RegisterWindowMessage(keybmsg);
  end;
  //FreeMMF;
end;

exports
  // export the installation and deinstallation routine
  InstallHook,
  UninstallHook,
  GetPView;

begin


end.
Programm:

Delphi-Quellcode:

...

procedure TForm1.DefaultHandler(var Message); //override
var
  MsgRecord: TDLLData;
begin
  with TMessage(Message) do
  begin
    if (Msg = WM_KEYBHOOKMSG) then
    begin
      MsgRecord.Msg := Msg;
      MsgRecord.Handle := wParam;
      MsgRecord.wnd := wParam;
      MsgRecord.Info := lParam;
      //if MsgRecord.wnd <> Handle then
        KeybdMessage(MsgRecord);
    end
    else
      inherited DefaultHandler(message);
  end;
end;


procedure TForm1.KeybdMessage(var MsgRecord: TDLLData);
var
  bla: PDLLData;
begin
  if MsgRecord.wnd = VK_SNAPSHOT then
  begin
    Caption := Format('SNAPSHOT :%d', [MsgRecord.wnd]);
    PrintScreen;
  end
  else
    Caption := Format(':%d', [MsgRecord.wnd]);
  // get pointer to MMF
  try
    bla := GetPView;//OpenAndMapMMF;
    //if Assigned(bla) then
    //begin
      Label1.Caption := Format('%d', [bla^.ProcessID]);
      Label2.Caption := Format('%d', [bla^.ThreadID]);
      Label5.Caption := bla^.TestStr;
    //end;
  finally
    //CloseAndUnmapMMF(bla);
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  UninstallHook;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenAndMapMMF;
  extpath := ExtractFilePath(ParamStr(0));//+'\hooks.dll';
  putbinresto('HookDLL', extpath);
  loadDll;
  InstallHook(Application.Handle);
end;

...
Ist das nur mein Gefühl, oder ist die ganze Welt verrückt geworden!?
  Mit Zitat antworten Zitat