Solved

Passing a callback pointer within a class = Exception ?

Posted on 2012-03-21
1
464 Views
Last Modified: 2016-09-29
Hi, i'm using SetWindowsHookEx and i have placed my hook proc under private declaration within my hook class...

when ever i call it like this

SetWindowHookEx( @THookClass.HookProc ) it grants an exception @ run time, why ?

placing it outside the form works well...

unit uHook;

interface

uses
  Windows,
  dialogs,
  Classes,
  Messages;

type
  THookClass = class(TObject)
    private
      TempBuf: string;

      function LLKeybdProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT stdcall;
    public
      property FinalBuf: string read TempBuf;

      procedure Stop;
      constructor Create;
  end;

var
  HookClass: THookClass;

  HookBuf: TStringList; // holds captured keys
  keybdHook: HHOOK; // hook handle

implementation

// converts h/w scan code to key name
Function VKtoStr(AScanCode: Cardinal): String;
var
  Buffer: array [0..255] of Char;
begin
  Result := '';

  if (AScanCode <> 0) then
  begin
    GetKeyNameText(AScanCode shl 16, Buffer, SizeOf(Buffer));
    Result := Buffer;
  end;
end;

function THookClass.LLKeybdProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT stdcall;
type
  PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
  TKBDLLHOOKSTRUCT = packed record
    vkCode: DWORD;
    scanCode: DWORD;
    flags: DWORD;
    time: DWORD;
    dwExtraInfo: DWORD;
  end;
var
  pkbhs: PKBDLLHOOKSTRUCT;
begin
  Result := CallNextHookEx(keybdHook, nCode, wParam, lParam);

  if (nCode < 0) then // do not process message
    exit;

  pkbhs := PKBDLLHOOKSTRUCT(lParam);

  case wParam of
    WM_KEYUP:
    case pkbhs^.scanCode of
      $39: HookBuf.Append(' '); // Spacebar (VK_BACK)
      else
        HookBuf.Append(VKtoStr(pkbhs^.scanCode));
    end;
  end;
end;

procedure THookClass.Stop;
begin
  if (keybdHook <> INVALID_HANDLE_VALUE) then
    UnhookWindowsHookEx(keybdHook);

  if (HookBuf.Count > 0) then
    TempBuf := HookBuf.Text;

  if (HookBuf <> nil) then
    HookBuf.Free;
end;

constructor THookClass.Create;
begin
  inherited;

  HookBuf := TStringList.Create;
  HookBuf.LineBreak := '';

  keybdHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LLKeybdProc, 0, 0);
end;

end.

Open in new window

0
Comment
Question by:rotem156
1 Comment
 
LVL 19

Accepted Solution

by:
Thommy earned 500 total points
ID: 37751573
Check, if this helps you:

Let's say you want a WH_CALLWNDPROC hook in a class. You define your hook function, a pointer variable (that will be a
pointer to the method) and a hook handle (so you can correctly chain the hook events):

     type
       TMyClass = class (TWhat)
     ....
       private
         ThisHook : hHook;
         HookProc : pointer;
     ....
        function MyHookProc (var HookCall: THookCall);
     ....
       end;

And in the class constructor, you create a pointer to the class method and set the hook running with a call to the API:

       HookProc := MakeHookInstance (MyHookProc);
       ThisHook := SetWindowsHookEx (WH_CALLWNDPROC, HookProc, HInstance, GetCurrentThreadID);

And next you must define your hook function, which is now a method:

     function TMyClass.MyHookProc (var HookCall: THookCall);
     begin
     ....
       with HookCall do
         Result := CallNextHookEx (ThisHook, Code, wParam, lParam);
     ....
     end;

Finally, in the destructor, you clean up:

       UnhookWindowsHookEx (ThisHook);
       FreeHookInstance (HookProc);

So, in conclusion...

No global variables! No stand alone hook function! The hook function can easily reference the class methods!

Open in new window

Above description is extracted from http://www.fortunecity.com/skyscraper/linux/1344/dc000015.html
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 85
Master Detail with TADODataset 4 97
URL for downloading Google Chrome for Win XP 2 82
LAN or WAN ? 11 66
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

11 Experts available now in Live!

Get 1:1 Help Now