Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   hook -> variable von form1 in dll bestimmen (https://www.delphipraxis.net/166743-hook-variable-von-form1-dll-bestimmen.html)

denizli 27. Feb 2012 20:45

hook -> variable von form1 in dll bestimmen
 
Hallo,

wie kann ich von der DLL auf die Variable von Form1 zugreifen ?

Hier der Code:

DLL
Delphi-Quellcode:
library jtdll;

uses
    Windows, Dialogs, 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
            if (wParam=VK_F5) then xyz:=1; // Hier die Zeile, die nicht funktioniert. Variable 'xyz' ist von Form1, die ich hier an der Stelle ändern möchte
        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.
Form1
Delphi-Quellcode:
var xyz: integer;

function InstallHook(AppHandle: HWND): Boolean; stdcall; external 'jtdll.dll';
function UninstallHook: Boolean; stdcall; external 'jtdll.dll';

procedure TForm1.FormCreate(Sender: TObject);
begin
    InstallHook(handle);
end;              

procedure TForm1.FormDestroy(Sender: TObject);
begin
    UninstallHook;
end;
LG

brechi 27. Feb 2012 21:27

AW: hook -> variable von form1 in dll bestimmen
 
Gar nicht, da die Dll im jweiligen Adressraum des anderen Prozesses geladen wird. Da hast du keinen Zugriff mehr auf deinen Speicherbereich. -> Lösung IPC

denizli 27. Feb 2012 21:51

AW: hook -> variable von form1 in dll bestimmen
 
Würde ClipBoard gehen/wäre es zu empfehlen ?
sprich: SetClipBoardData(), GetClipBoardData()
Ich brauche nur eine Variable, die ich in der DLL bestimmen möchte.

LG

Sir Rufo 27. Feb 2012 22:00

AW: hook -> variable von form1 in dll bestimmen
 
Zitat:

Zitat von brechi (Beitrag 1153267)
Gar nicht, da die Dll im jweiligen Adressraum des anderen Prozesses geladen wird. Da hast du keinen Zugriff mehr auf deinen Speicherbereich. -> Lösung IPC

Es gibt doch aber noch die Möglichkeit des CallBacks ;)

himitsu 27. Feb 2012 22:18

AW: hook -> variable von form1 in dll bestimmen
 
Abgesehn davon:

DLL und EXE haben ihre eigene RTTI und deswegen tut man überhaupt nicht grenzübergreifend (EXE<>DLL / DLL<>DLL) auf Objekte des anderen Seite zugreifen. :warn:

(selbst wenn hier, wie bereits genannt, auch noch Prozessgrenze dazukommt)

UliBru 28. Feb 2012 07:42

AW: hook -> variable von form1 in dll bestimmen
 
Wenn ich mir den JVCL Plugin-Manager anschaue, z.B. das Beispielprojekt JvPlugin - ChangingProperties, dann zeigt das doch wie so was geht.
Oder liege ich da falsch?

Blup 1. Mär 2012 10:01

AW: hook -> variable von form1 in dll bestimmen
 
Deiner DLL wird bereits ein WindowHandle übergeben, ist dieses das Handle der Form1 kannst du per SendMessage oder PostMessage komunizieren und die Variable so indirekt setzen. Eine andere Möglichkeit wäre ein Interface zu verwenden.
Delphi-Quellcode:
unit KeyboardF5;

type
  IKeyboardF5 = interface(IInterface)
    function GetF5Variable: Integer;
    procedure SetF5Variable(AValue: Integer);
  end

//---------

library jtdll;

uses
  Windows, Dialogs, Messages, KeyboardF5;

var
  HookHandle: Cardinal;
  KeyboardF5: IKeyboardF5;

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
        if (wParam=VK_F5) and Assigned(KeyboardF5) then
          KeyboardF5.SetF5Variable(1);
      end;
  end;
end;

function InstallHook(AKeyboardF5: IKeyboardF5): Boolean; stdcall;
begin
  Result := (HookHandle = 0);
  if Result then
  begin
    HookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, HInstance, 0);
    KeyboardF5 := AKeyboardF5;
  end;
end;

function UninstallHook: Boolean; stdcall;
begin
  Result    := UnhookWindowsHookEx(HookHandle);
  HookHandle := 0;
  KeyboardF5 := nil;
end;

exports
  InstallHook,
  UninstallHook;

end.

//---------

unit Form1;

uses
  {...}
  KeyboardF5;

type
  TForm1 = class(TForm, IKeyboardF5)
    {...}
  private
    xyz: Integer;
    function GetF5Variable: Integer;
    procedure SetF5Variable(AValue: Integer);
  end;

implementation

function TForm1.GetF5Variable: Integer;
begin
  Result := xyz;
end;

procedure TForm1.SetF5Variable(AValue: Integer);
begin
  xyz := AValue;
end;

procedure TForm1.Show(Sender: TObject);
begin
  InstallHook(Self);
end;

procedure TForm1.Close(Sender: TObject);
begin
  UnInstallHook;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:32 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