SetWindowsHookEx dll not works

Good evening, I made a dll in Delphi, and in it I do block some keys, I start this dll using rund32.exe, this same DLL calls also one way to alert warning of the incident, what happens that I make form mentioned Assima the lock button does not occur if I do this code within an .exe the code already works perfectly, I believe it is the hInstance, most do not know how to pass him by … down this dll used the code in dll

type
  tagKBDLLHOOKSTRUCT = record
  vkCode: DWord;
  scanCode: DWord;
  flags: DWord;
  time: DWord;
  dwExtraInfo: PDWord;
end;

  TKBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
  PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;

var
  frmAlert: TfrmAlert;
  khk: HHOOK;


function KeyboardHookProc(Code: Integer; wParam : WPARAM; lParam : LPARAM): NativeInt; stdcall;
var
  p:PKBDLLHOOKSTRUCT;
begin
  p := PKBDLLHOOKSTRUCT(lParam);

  if (Code = HC_ACTION) and (wParam = $0100) then
    if (p.vkCode = VK_MENU) or (p.vkCode = VK_LWIN) or (p.vkCode = VK_RWIN)or (p.vkCode = VK_ESCAPE) or (p.vkCode = VK_TAB) then
      Result := 1
    else
      Result := CallNextHookEx(0, Code, wParam, lParam);
end;


procedure TfrmAlert.FormCreate(Sender: TObject);
begin

  khk                := SetWindowsHookEx(13, KeyboardHookProc,hinstance ,0);

end;

procedure TfrmAlert.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  UnhookWindowsHookEx(khk);
  ClipCursor(nil);
  Action     := CaFree;
  frmAlert    := nil;
end;


Source: dll

Leave a Reply