Re: how to convert a member function to a global function?
"David Wilkinson" <no-reply@effisols.com> wrote in message
news:%23eVuPk3HHHA.3312@TK2MSFTNGP03.phx.gbl...
Bill Gates wrote:
For example, SetWindowsHookEx need input a HOOKPROC argument.
There is a class like the following,
class foo
{
HHOOK m_hook;
public:
// REMARK: hookproc is not static type
LRESULT hookproc( int nCode, WPARAM wParam, LPARAM lParam )
{
// do something...
return ::CallNextHookEx( m_hook, nCode, wParam, lParam );
}
};
Is there a way to convert the foo::hookproc to HOOKPROC type to transport
to SetWindowsHookEx?
Bill:
Yes, SetWindowsHookEx() does not have a "LPVOID" parameter that can be
used to pass context. Outrageous.
A "poor man's way" to do this is to make the callback static (as it has to
be) and have static member variable "foo* m_pThis" that can point to your
instance. Before calling SetWindowsHookEx(), set m_pThis equal to the
"this" pointer (assuming you are calling it from with your class foo).
Your static callback can now use "m_pThis" to transfer the call to a
non-static member function.
Although this really only works for one instance, if hooks are system-global
in the way I think they are (all hooks receive the same set of messages),
then you can use a list of instances and call each one from the static
callback.
This will work, but your code is no longer thread-safe. You must be sure
that only one thread ever calls SetWindowsHookEx() in this way, or that
there can only be one foo object. For practical use, this is not really a
problem.
David Wilkinson