Recent

Author Topic: Parameters of setwindowshookex  (Read 6506 times)

bruce.button

  • Jr. Member
  • **
  • Posts: 59
Parameters of setwindowshookex
« on: March 24, 2014, 10:43:48 pm »
I have the following line of code:

CurrentHook:=setwindowshookex(WH_KEYBOARD,@KeyBoardHook,0,GetCurrentThreadID());

where @KeyBoardHook is a function defined in the same unit.

When compiling I get the error:

Local.pas(27,60) Error: Incompatible type for arg no. 2: Got "<address of function(LongInt,Word,LongWord):DWord;StdCall>", expected "<procedure variable type of function(LongInt,LongInt,LongInt):LongInt;StdCall>"

As far as I can gather from MSDN, the second parameter of setwindowshookex should indeed be a pointer to a function with the arguments as specified.

Any ideas?

Thanks!

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Parameters of setwindowshookex
« Reply #1 on: March 25, 2014, 01:17:16 am »
According to the error message your KeyBoardHook signature is not compatible with SetWindowsHookEx because you used Word (2 bytes) for the second parameter while the needed one is of type LongInt (4 bytes). Similar problem with the third parameter.

The correct KeyBoardHook, I believe, is:
Code: [Select]
uses
..., Windows;
...
function KeyboardHook(_para1: LongInt; _para2: WPARAM; _para3: LPARAM): LRESULT; stdcall;

bruce.button

  • Jr. Member
  • **
  • Posts: 59
Re: Parameters of setwindowshookex
« Reply #2 on: March 25, 2014, 01:35:15 pm »
Thank you for that clarification. What puzzled me was that SetWindowsHookEx is a Windows API function — defined by Windows itself — which made me wonder how FPC could use different parameter types. Does this mean that Lazarus/Free Pascal has its own KeyboardHook function which interfaces with the Windows API behind the scenes?

(Just for interest's sake: the code I posted above works in Delphi mode {$mode delphi}.)

Thanks again!

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Parameters of setwindowshookex
« Reply #3 on: March 25, 2014, 03:23:34 pm »
What puzzled me was that SetWindowsHookEx is a Windows API function — defined by Windows itself — which made me wonder how FPC could use different parameter types.
FPC does not use different parameter types. You do not mean parameter names, or do you?

Does this mean that Lazarus/Free Pascal has its own KeyboardHook function which interfaces with the Windows API behind the scenes?
No, simply press and hold your control button (Ctrl) and move your mouse pointer to SetWindowsHookEx when it turns blue, like a link, click on it. That should take you to SetWindowsHookEx declaration.

Windows provides dynamic link libraries (dlls) to share procedures like SetWindowsHookEx. To link with one of these libraries (user32.dll), FPC uses the following code (32 bit):
Code: [Select]
function SetWindowsHookEx(idHook:longint; lpfn:HOOKPROC; hmod:HINST; dwThreadId:DWORD):HHOOK; external 'user32' name 'SetWindowsHookExA';
the preceding code uses ANSI version of SetWindowsHookEx. The real name for this version inside user32.dll is SetWindowsHookExA.

bruce.button

  • Jr. Member
  • **
  • Posts: 59
Re: Parameters of setwindowshookex
« Reply #4 on: March 25, 2014, 03:52:19 pm »
Quote
FPC does not use different parameter types. You do not mean parameter names, or do you?

Sorry, I was getting mixed up between the SetWindowsHookEx function and the KeyboardHook function!

Thanks for the other clarifications about how FPC links with the Windows libraries.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Parameters of setwindowshookex
« Reply #5 on: March 31, 2014, 06:25:40 pm »
(note that WPARAM was 2 bytes in windows 3.11. Maybe Delphi kept compatibility to that longer because of D1. Best is to simply type it with WPARAM and LPARAM, since they also have other sizes in 64-bit)

bruce.button

  • Jr. Member
  • **
  • Posts: 59
Re: Parameters of setwindowshookex
« Reply #6 on: March 31, 2014, 08:27:42 pm »
Thanks, Marcov, I will bear that in mind.

 

TinyPortal © 2005-2018