🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
9 messages
9 total messages Started by amol Wed, 19 Mar 2008 05:36
How to use SetWindowsHookEx?
#499
Author: amol
Date: Wed, 19 Mar 2008 05:36
25 lines
1368 bytes
Hi there,

I am getting following error messages while using SetWindowsHookEx:

"cannot convert 'long (__stdcall *)(int,unsigned int,long)' 
to 'int(__stdcall *)()' "

"Type mismatch in parameter 'lpfn' (wanted 'int(__stdcall *)()', got 'long(__stdcall *) (int,unsigned int,long)')"


I am calling the "SetWindowsHookEx" as follow:

hook = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hinstance,NULL);
LRESULT CALLBACK KeyboardProc(int ncode,WPARAM wparam,LPARAM lparam)
{
  return ( CallNextHookEx(hook,ncode,wparam,lparam) );
}

As per the documentation "KeyboardProc" should take three parameters, but it is giving me error.
I am using C++ Builder 6.

Thanks,

Amol
Re: How to use SetWindowsHookEx?
#501
Author: Bob Gonder
Date: Wed, 19 Mar 2008 14:14
10 lines
1241 bytes
amol wrote:

>"Type mismatch in parameter 'lpfn' (wanted 
>	'int(__stdcall *)()'
>, got
>	'long(__stdcall *) (int,unsigned int,long)')"

>hook = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hinstance,NULL);

hook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hinstance,NULL);
Re: How to use SetWindowsHookEx?
#500
Author: Darko Miletic
Date: Wed, 19 Mar 2008 14:29
25 lines
1957 bytes
amol wrote:
> Hi there,
> 
> I am getting following error messages while using SetWindowsHookEx:
> 
> "cannot convert 'long (__stdcall *)(int,unsigned int,long)' 
> to 'int(__stdcall *)()' "
> 
> "Type mismatch in parameter 'lpfn' (wanted 'int(__stdcall *)()', got 'long(__stdcall *) (int,unsigned int,long)')"
> 
> 
> I am calling the "SetWindowsHookEx" as follow:
> 
> hook = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hinstance,NULL);
> LRESULT CALLBACK KeyboardProc(int ncode,WPARAM wparam,LPARAM lparam)
> {
>   return ( CallNextHookEx(hook,ncode,wparam,lparam) );
> }

You must cast the function pointer to HOOKPROC like this:

hook = SetWindowsHookEx(  WH_KEYBOARD
                         , reinterpret_cast<HOOKPROC>(KeyboardProc)
                         , hinstance
                         , 0U);
Re: How to use SetWindowsHookEx?
#504
Author: Remy Lebeau (TeamB)
Date: Wed, 19 Mar 2008 16:54
13 lines
1238 bytes
"amol" <kolekar.amol@gmail.com> wrote in message 
news:47e0a645$1@newsgroups.borland.com...

> I am getting following error messages while using SetWindowsHookEx:

You need to type-cast the function pointer that you are passing in:

    hook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) &KeyboardProc, 
hinstance, NULL);


Gambit 
Re: How to use SetWindowsHookEx?
#507
Author: Ed Mulroy [TeamB]
Date: Wed, 19 Mar 2008 21:33
48 lines
2344 bytes
Better to not use a cast.

Instead:

What HOOKPROC is (from winuser.h)

typedef LRESULT (CALLBACK* HOOKPROC)(
     int code,
     WPARAM wParam,
     LPARAM lParam);

Change your function to be

LRESULT CALLBACK KeyboardProc(int ncode, wParam wparam, lParam lparam)

so that it matches the type for which SetWindowsHookEx is supposed to 
receive.  You are passing an HINSTANCE to the wParam argument so cast that 
calling parameter.  Do not cast the whole function as that masks changes 
made in any future edits and they can have devastating effects, for example: 
forgetting to label the function as CALLBACK or __stdcall.

.  Ed

> amol wrote in message
> news:47e0a645$1@newsgroups.borland.com...
>
> Hi there,
>
> I am getting following error messages while using SetWindowsHookEx:
>
> "cannot convert 'long (__stdcall *)(int,unsigned int,long)'
> to 'int(__stdcall *)()' "
>
> "Type mismatch in parameter 'lpfn' (wanted 'int(__stdcall *)()', got 
> 'long(__stdcall *) (int,unsigned int,long)')"
>
>
> I am calling the "SetWindowsHookEx" as follow:
>
> hook = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hinstance,NULL);
> LRESULT CALLBACK KeyboardProc(int ncode,WPARAM wparam,LPARAM lparam)
> {
>  return ( CallNextHookEx(hook,ncode,wparam,lparam) );
> }
>
> As per the documentation "KeyboardProc" should take three parameters, but 
> it is giving me error.
> I am using C++ Builder 6.
Re: How to use SetWindowsHookEx?
#508
Author: Remy Lebeau (TeamB)
Date: Wed, 19 Mar 2008 22:16
47 lines
2299 bytes
"Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket> wrote in message 
news:47e186a4@newsgroups.borland.com...

> What HOOKPROC is (from winuser.h)
>
> typedef LRESULT (CALLBACK* HOOKPROC)(
>     int code,
>     WPARAM wParam,
>     LPARAM lParam);

It is only declared that way when STRICT is defined, which is usually not 
the case (it is not in amol's, as evident by the error message he showed). 
The full declaration is as follows:

    #ifdef STRICT
    typedef LRESULT (CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM 
lParam);
    #else
    typedef FARPROC HOOKPROC;
    #endif

FARPROC is defined in windef as follows:

    #ifdef _WIN64
    typedef INT_PTR (FAR WINAPI *FARPROC)();
    #else
    typedef int (FAR WINAPI *FARPROC)();
    #endif

> Change your function to be
>
> LRESULT CALLBACK KeyboardProc(int ncode, wParam wparam, lParam lparam)
>
> so that it matches the type for which SetWindowsHookEx is supposed to 
> receive.

His original declaration already matches what a keyboard hook is supposed to 
use.  Your change, on the other hand, won't compile.

> You are passing an HINSTANCE to the wParam argument

No, he's not.  He's passing an HINSTANCE to SetWindowsHookEx(), not 
KeyboardProc().


Gambit 
Re: How to use SetWindowsHookEx?
#509
Author: Minas
Date: Thu, 20 Mar 2008 00:27
12 lines
1248 bytes
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote:

> You need to type-cast the function pointer that you are passing in:
>
>    hook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) &KeyboardProc, 
> hinstance, NULL);

just a small comment on (HOOKPROC) &KeyboardProc , the reference operator & 
doesn't need since the function name is pointer .

Best Regards
Minas 
Re: How to use SetWindowsHookEx?
#510
Author: amol
Date: Thu, 20 Mar 2008 05:13
5 lines
885 bytes

Thanks to all for the prompt help.

Amol
Re: How to use SetWindowsHookEx?
#511
Author: Anders Johansen
Date: Thu, 20 Mar 2008 10:27
4 lines
977 bytes
Perhaps you should have a look at the code I posted a few weeks ago in this newsgroup?

Anders
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads