Solved

delphi SetWindowsHookEx inside thread

Posted on 2013-12-03
5
1,004 Views
Last Modified: 2013-12-05
Hi, im trying to hook the keyboard inside a thread.
The code works fine on the VLC form but inside the thread the hook wont work

the code is below

 type
  tagKBDLLHOOKSTRUCT = packed record // contém as informações do evento
    vkCode: DWord; // virtual key (1.254) vk_back,vk_return,etc
    scanCode: DWord;
    flags: DWord;
    time: DWord;
    dwExtraInfo: PDWord;
  end;

  TKBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
  PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;

type
  Thookbarcode = class(TThread)
  private
    apppath : string;
  protected
    procedure Execute; override;
  public
    hookhandle: HHook;
    constructor create(meubarcode : Tbarcodereader; apppath : string);
  end;

function LowLevelKeyboardHook(Code: integer; wParam: wParam; lParam: lParam)
    : longint;  stdcall;

var
  codigo: string;
  intervalo : cardinal;
  threadbarcode : Tbarcodereader;


implementation

constructor Thookbarcode.create(meubarcode: Tbarcodereader; apppath : string);
begin
  inherited create(true);
  threadbarcode := meubarcode;
  self.apppath := apppath;
  resume;

end;

function LowLevelKeyboardHook(Code: Integer; wParam: wParam; lParam: lParam)
  : longint; stdcall;
var
  p: PKBDLLHOOKSTRUCT;
begin
  p := PKBDLLHOOKSTRUCT(lParam);
  if (p.vkCode = VK_RETURN) then
  begin
  result := 1;
  exit;
  end;
  if wParam = $0100 then
  begin
    if (p.vkCode >= 48) and (p.vkCode <= 57) then
    begin
      if gettickcount - intervalo < 50 then
        codigo := codigo + Char(p.vkCode)
      else
        codigo := Char(p.vkCode);
      if length(codigo) > 7 then
      begin
        threadbarcode.add(codigo);
        codigo := '';
      end;
      Result := 1;
      intervalo := gettickcount;
      exit;
    end;
  end;
  Result := CallNextHookEx(0, Code, wParam, lParam);
end;

procedure Thookbarcode.Execute;
begin
hookhandle := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardHook, Hinstance, 0);
 while not terminated do
 begin
   sleep(1);
 end;
 UnHookWindowsHookEx(hookhandle);
end;

Open in new window

0
Comment
Question by:Slinkkkk
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 39694826
0
 
LVL 19

Assisted Solution

by:MerijnB
MerijnB earned 334 total points
ID: 39694989
Do you use TThread? It's possible the hook needs a message loop to work correctly.
What else is in this thread?
0
 
LVL 19

Assisted Solution

by:Thommy
Thommy earned 166 total points
ID: 39695095
Probably that's the point!!!

If you call a hook in the context of a thread, the thread that installed the hook must have a message loop.

For how to implement a message loop for a thread, have a look at Unit HookThread
0
 
LVL 19

Accepted Solution

by:
MerijnB earned 334 total points
ID: 39695169
Alternatively you can add such a method to your TThread descendant:

procedure _TThread.MessageLoop;
var Msg: TMsg;
begin
 // If GetMessage retrieves the WM_QUIT, the return value is FALSE and the message loop is broken.

 while GetMessage(Msg, 0, 0, 0) do
 begin
  TranslateMessage(Msg);
  DispatchMessage(Msg)
 end;

 inherited Terminate();
end;

Open in new window


and call MessageLoop() in your execute method:
procedure _TThread.Execute;
begin
 // do init stuff

 MessageLoop();

 // do deinit stuff
end;

Open in new window


You'll have to keep in mind that your thread now isn't a sync function anymore, hence my question what else you are doing in your thread.
0
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 39695275
Why do you need threading? It can be done without thread. All hooking stuff should be placed in external dll. Hooking procedure must take callback procedure - which is working like event procedure.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now