setwindowshookex Incompatible type for arg (SOLVED) lazarus / 62-General ---------------------------------------------------------------------- TheLastCayen · 2019-11-13 23:47 Hi I am using:  - Windows 7 64 bit  - Lazarus 2.0.6  - FPC 3.0.4 I am using this function from windows:   kHook := setwindowshookex(WH_KEYBOARD,@LowLevelKeybdHookProc,0,GetCu rrentThreadID()); This is my function:   function LowLevelKeybdHookProc(nCode: LongInt; WPARAM: WPARAM; lParam : LPARAM) : LRESULT; stdcall; This is my error message: Error: Incompatible type for arg no. 2: Got "", expected "" I found this function on that post: https://forum.lazarus.freepascal.org/index.php?topic=24023.0 but also from many other sources on the web. Anyone know why it's not working anymore and how can I fix it? Thank you ---------------------------------------------------------------------- 440bx · 2019-11-14 00:47 Quote from: TheLastCayen on November 14, 2019, 12:47:17 amThis is my error message: Error: Incompatible type for arg no. 2: Got "", expected "" The error message indicates that you are passing the address of a class method (indicated by "of object".)  The hook procedure _cannot_ be a class method, it _must_ be a normal function. ---------------------------------------------------------------------- jamie · 2019-11-14 02:14 I believe a CLASS PROCEDURE or FUNCTION will also work if the compiler will accept the STDCALL attached on the end of it ---------------------------------------------------------------------- balazsszekely · 2019-11-14 04:31 @TheLastCayen Replace {$mode objfpc}{$H+} with {$mode delphi}. Check the following thread for more details: https://forum.lazarus.freepascal.org/index.php/topic,37049.0.html ---------------------------------------------------------------------- Thaddy · 2019-11-14 05:04 Quote from: jamie on November 14, 2019, 03:14:23 amI believe a CLASS PROCEDURE or FUNCTION will also work if the compiler will accept the STDCALL attached on the end of it  with the static modifier. and yes, stdcall will be accepted. ---------------------------------------------------------------------- PascalDragon · 2019-11-14 08:14 Quote from: GetMem on November 14, 2019, 05:31:24 am@TheLastCayen Replace {$mode objfpc}{$H+} with {$mode delphi}. Check the following thread for more details: https://forum.lazarus.freepascal.org/index.php/topic,37049.0.html That won't make a difference. The problem here is instance method vs. static/global function. ---------------------------------------------------------------------- balazsszekely · 2019-11-14 10:09 @PascalDragon QuoteThat won't make a difference. The problem here is instance method vs. static/global function.True. I did not read the error message carefully. Nevertheless the above mentioned thread still has a nice example on hooking(for those who are interested). ---------------------------------------------------------------------- TheLastCayen · 2019-11-14 22:39 Thank you for all those answers.  When I look at LRESULT , it's defined in base.inc : Line 193 : LRESULT = LONG_PTR;  LONG_PTR define in base,inc : Line 89: LONG_PTR = PtrInt; PtrInt is define in systemh.inc: Line 338 PtrInt = Int64; At the end, LRESULT should be an int64, not Int64 of object... I have a hard time understanding what I am doing wrong:( someone can tell me how I can modify this function so it will work? Thank you ---------------------------------------------------------------------- PascalDragon · 2019-11-15 08:10 Quote from: TheLastCayen on November 14, 2019, 11:39:04 pmI have a hard time understanding what I am doing wrong:( someone can tell me how I can modify this function so it will work? Your function LowLevelKeybdHookProc is contained inside a class (probably your form class), thus it is an instance method, but it must not be such. So either move your function to outside of the class into the implementation section of your unit or declare your function as class function LowLevelKeybdHookProc(...): LRESULT; stdcall; static. Please note however that in both cases you can not directly access your class using Self. ---------------------------------------------------------------------- Thaddy · 2019-11-15 08:51 Indeed, and you can pass self  as the lparam. See the documentation on msdn. (maybe in some cases self.handle, i need to check that) And use PtrUint as the type to keep 32/64 bit compatibility. You are on 64 bit and compiling for 64 bit, it seems. ---------------------------------------------------------------------- TheLastCayen · 2019-11-15 22:42 Thank you for your reply. Programming for windows using windows dll is always a nightmare:( My code is already working well under Linux and I start considering dropping the windows part:( When I add static, I get that error message: Error: Directive "STATIC" not allowed here If I try to write the code for that function over implementation I get Fatal: Syntax error, "IMPLEMENTATION" expected but "BEGIN" found I should have mentioned, my lazarus/FPC are also 64bit. I am getting really desperate here. Any other recommendations? ---------------------------------------------------------------------- 440bx · 2019-11-15 23:22 Quote from: TheLastCayen on November 15, 2019, 11:42:28 pmI am getting really desperate here. Any other recommendations? Yes, post the entire code of your hook function.   That would help figure out why you're having trouble with it. ---------------------------------------------------------------------- lucamar · 2019-11-15 23:40 Quote from: TheLastCayen on November 15, 2019, 11:42:28 pmIf I try to write the code for that function over implementation I get Fatal: Syntax error, "IMPLEMENTATION" expected but "BEGIN" found not over implementation but under it. That is, the function body (definition) should be inside the implementation section, as in: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i implementation function LowLevelKeybdHookProc(nCode: LongInt; WPARAM: WPARAM; lParam : LPARAM) : LRESULT; stdcall;begin  { Do something }end; ---------------------------------------------------------------------- jamie · 2019-11-16 00:01 This is against my religion but here goes.. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Windows, Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type   { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private   public  HookHandle:Int64;  Class Function AHookProc(nCode:LongInt; awParam:WPARAM;aLparam:LParam):LRESULT; StdCall; Static;  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.FormCreate(Sender: TObject);begin  HookHandle := SetWindowsHookEx(WH_KEYBOARD, HookPROC(@TForm1.AHookProc),HInstance, GetThreadID);end; class Function Tform1.AHookProc(nCode:LongInt; awParam:WPARAM;aLparam:LParam):LRESULT; StdCall;Begin // Hit a key...  Beep; Result :=  CallnextHookEx(Form1.HookHandle,nCode,AwParam, ALparam);end; end. ---------------------------------------------------------------------- TheLastCayen · 2019-11-16 04:20 Thank you for your help everyone. Jamie, with your code I can finally compile without an error message:) ---------------------------------------------------------------------- jamie · 2019-11-16 22:04 Yeah no problem, I'll most likely get castrated for giving you that bit of info. ---------------------------------------------------------------------- PascalDragon · 2019-11-17 09:53 Quote from: jamie on November 16, 2019, 11:04:49 pmYeah no problem, I'll most likely get castrated for giving you that bit of info. Why? It was one of the two solutions suggested by me as well. ---------------------------------------------------------------------- 440bx · 2019-11-17 10:39 Quote from: PascalDragon on November 17, 2019, 10:53:58 amWhy? It was one of the two solutions suggested by me as well. Because he bought cheap real estate, consequently he gets bad schools and extreme neighbors ---------------------------------------------------------------------- jamie · 2019-11-17 16:24 You may have a point there but the real-estate here isn't cheap although we just don't seem to use any of our tax money at work.. I think it has something to do with over paid gov workers and too many free lunch programs. ---------------------------------------------------------------------- Remy Lebeau · 2019-11-22 02:58 Quote from: Thaddy on November 15, 2019, 09:51:58 amIndeed, and you can pass self  as the lparam. No, you can't, because SetWindowsHookEx() does not allow you to pass any user-defined values to its hooks.  So you will have to use either a global variable or a thunk to get the Self pointer into the hook procedure.