Parameters of setwindowshookex
lazarus / 34-Windows
----------------------------------------------------------------------
bruce.button · 2014-03-24 21:43
I have the following line of code:
CurrentHook:=setwindowshookex(WH_KEYBOARD,@KeyBoardHook,0,GetCurrentTh
readID());
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 "
", expected
""
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 · 2014-03-25 00:17
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 · 2014-03-25 12:35
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 · 2014-03-25 14:23
Quote from: bruce.button on March 25, 2014, 01:35:15 pmWhat 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?
Quote from: bruce.button on March 25, 2014, 01:35:15 pmDoes 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 · 2014-03-25 14:52
QuoteFPC 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 · 2014-03-31 16:25
(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 · 2014-03-31 18:27
Thanks, Marcov, I will bear that in mind.