Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! Is it possible to use MouseProc callback function as a class member function?
C++
//class declaration
//...
private:
LRESULT CALLBACK MouseWndProc(int nCode, WPARAM wParam, LPARAM lParam);
//...

And when I try to pass a MouseWndProc function pointer as the second parameter of the function SetWindowsHookEx:
C++
SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseWndProc, //...

here I get an error: member function must be called or its address taken


[edit]

I changed the code as follows:

C++
//declaration
class MyClass
{
public:
void MyClass();
void ~MyClass();
static LRESULT CALLBACK MouseWndProc(int nCode, WPARAM wParam, LPARAM lParam);

private:
int x;
int y;
};
//and implementation:

LRESULT CALLBACK MyClass::MouseWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
switch(wParam)
	{
	case WM_MOUSEMOVE:
		{

		x = pMouseStruct->pt.x;
		y = pMouseStruct->pt.y;
		break;
		}

	}
	return CallNextHookEx(hhk, nCode, wParam, lParam);
}


And when I try to compile this code, on this lines:
C++
x = pMouseStruct->pt.x;
y = pMouseStruct->pt.y;


I have got an error:

Member MyClass::x cannot be used without an object
Member MyClass::y cannot be used without an object


How to fix this?

[/edit]
Posted
Updated 9-Sep-14 1:08am
v2
Comments
Stefan_Lang 9-Sep-14 5:31am    
Remove the typecast and you will get an actually useful error message that is hidden by the cast. In short: a member function pointer is not a function pointer and cannot be converted into one, not even by a (wrongly used) C-style type cast. You've got to remember that a member function always implicitely adds the object instance to the argument list! The only exception, as Richard stated in his solution, is static member functions. (but then, why make it a member function at all?)
Igor-84 9-Sep-14 6:23am    
I need it to work with class member variables inside MouseWndProc
Richard MacCutchan 9-Sep-14 7:11am    
You cannot refer to instance variables in a static function, without an object reference. You need to pass a pointer to the current object in your parameters somewhere.
Igor-84 9-Sep-14 7:58am    
Richard MacCutchan, write a simple example please.

1 solution

The member function must be static to be used in this way.
 
Share this answer
 
Comments
Igor-84 9-Sep-14 6:17am    
Comments moved to question.
Richard MacCutchan 9-Sep-14 7:10am    
See my comment above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900