Solved

Passing a callback pointer within a class = Exception ?

Posted on 2012-03-21
1
499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

763 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