Einzelnen Beitrag anzeigen

Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.435 Beiträge
 
Delphi 10.4 Sydney
 
#7

AW: hook -> variable von form1 in dll bestimmen

  Alt 1. Mär 2012, 10:01
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;
  Mit Zitat antworten Zitat