AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Kommunikation mit DLL

Ein Thema von Coder1990 · begonnen am 28. Jul 2008 · letzter Beitrag vom 28. Jul 2008
Antwort Antwort
Benutzerbild von Coder1990
Coder1990

Registriert seit: 2. Nov 2007
116 Beiträge
 
Delphi 2005 Personal
 
#1

Kommunikation mit DLL

  Alt 28. Jul 2008, 15:59
Seas,

Anmerkung:
Hoffe ich bin hier im richtigen Subforum

//Edit: Hab "RegisterHotkey" übersehen (viel einfacher ) trotzdem danke


Problemstellung:
Ich bin gerade dabei ein Script - Programm zu schreiben, dazu verwende ich systemweite Hooks. Nun sollen meine Scripts per Hotkey gestartet werden können und da ich da ich zu dem Zeitpunkt nunmal eh schon einen systemweiten Hook registriert habe, möchte ich diesen auch gleich zum Abfangen der Hotkeys nutzen. Meine Hook DLL sendet per PostMessage die gedrückte Taste an mein Programm, aber ich weiß nicht wie ich eine Information (Tastendruck blockieren oder nicht) an die DLL zurücksenden kann.

Dazu hier mein DLL Quelltext (entnommen aus SwissDelphiCenter und modifiziert):
Delphi-Quellcode:
library Hk;

uses
  Windows,
  Messages,
  SysUtils;

type
  PHookRec = ^THookRec;
  THookRec = record
    AppHnd: Integer;
  end;

var
  Hooked: Boolean;
  hKeyHook, hMemFile, hApp: HWND;
  PHookRec1: PHookRec;

function KeyHookFunc(Code, VirtualKey, KeyStroke: Integer): LRESULT; stdcall;
var
  KeyState1: TKeyBoardState;
  AryChar: array[0..1] of Char;
  Count: Integer;
begin
  Result := 0;
  if Code = HC_NOREMOVE then Exit;
  Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke);
  {I moved the CallNextHookEx up here but if you want to block
   or change any keys then move it back down}

  if Code < 0 then
    Exit;

  if Code = HC_ACTION then
  begin
    if ((KeyStroke and (1 shl 30)) <> 0) then
      begin
       {I moved the OpenFileMapping up here so it would not be opened
        unless the app the DLL is attatched to gets some Key messages}

        hMemFile := OpenFileMapping(FILE_MAP_WRITE, False, 'Global7v9k');
        PHookRec1 := MapViewOfFile(hMemFile, FILE_MAP_WRITE, 0, 0, 0);
        if PHookRec1 <> nil then
        begin
          hApp := PHookRec1.AppHnd;
        end;
      end;
    if ((KeyStroke and (1 shl 30)) <> 0) then
    begin
      GetKeyboardState(KeyState1);
      Count := ToAscii(VirtualKey, KeyStroke, KeyState1, AryChar, 0);
      if Count = 1 then
      begin
        {I included 2 ways to get the Charaters, a Memo Hnadle and
         a WM_USER+1678 message to the program}

        PostMessage(hApp, WM_USER + 1678, Ord(AryChar[0]), 0);
      end;
    end;
  end;
end;


function StartHook(AppHandle: HWND): Byte; export;
begin
  Result := 0;
  if Hooked then
  begin
    Result := 1;
    Exit;
  end;
  hKeyHook := SetWindowsHookEx(WH_KEYBOARD, KeyHookFunc, hInstance, 0);
  if hKeyHook > 0 then
  begin
    {you need to use a mapped file because this DLL attatches to every app
    that gets windows messages when it's hooked, and you can't get info except
     through a Globally avaiable Mapped file}

    hMemFile := CreateFileMapping($FFFFFFFF, // $FFFFFFFF gets a page memory file
      nil, // no security attributes
      PAGE_READWRITE, // read/write access
      0, // size: high 32-bits
      SizeOf(THookRec), // size: low 32-bits
      //SizeOf(Integer),
      'Global7v9k'); // name of map object
    PHookRec1 := MapViewOfFile(hMemFile, FILE_MAP_WRITE, 0, 0, 0);
    hApp := AppHandle;
    PHookRec1.AppHnd := AppHandle;
    {set the Memo and App handles to the mapped file}
    Hooked := True;
  end
  else
    Result := 2;
end;

function StopHook: Boolean; export;
begin
  if PHookRec1 <> nil then
  begin
    UnmapViewOfFile(PHookRec1);
    CloseHandle(hMemFile);
    PHookRec1 := nil;
  end;
  if Hooked then
    Result := UnhookWindowsHookEx(hKeyHook)
  else
    Result := True;
  Hooked := False;
end;

procedure EntryProc(dwReason: DWORD);
begin
  if (dwReason = Dll_Process_Detach) then
  begin
    if PHookRec1 <> nil then
    begin
      UnmapViewOfFile(PHookRec1);
      CloseHandle(hMemFile);
    end;
    UnhookWindowsHookEx(hKeyHook);
  end;
end;

exports
  StartHook,
  StopHook;

begin
  PHookRec1 := nil;
  Hooked := False;
  hKeyHook := 0;
  DLLProc := @EntryProc;
  EntryProc(Dll_Process_Attach);
end.
Problem noch mal auf den Punkt:
Wie sende ich eine Boolean Information an meine DLL ohne dabei, zu viel Zeit zu Verschwenden?

MfG
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#2

Re: Kommunikation mit DLL

  Alt 28. Jul 2008, 22:04
Such dir was aus: Pipes, Mailslots, MemoryMappedFiles, ...
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:56 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz