[RESOLVED]Callback Type Problem
First off, I'm a newbie C++ coder, so I'm sorry if what I'm asking is obvious, but searching has given me no solution. I'm trying to write a simple window class with the message handler passed by the class user, so I define a member variable as "LRESULT(*)(HWND, UINT, WPARAM, LPARAM)", which is supposed to be the function pointer. Then I assign the passed value(same type) to this member, which results in the error: "invalid conversion from `LRESULT (*)(HWND__*, UINT, WPARAM, LPARAM)' to `LRESULT (*)(HWND__*, UINT, WPARAM, LPARAM)'". I read that it could be caused by the function address being that of a non-static member function, but I've tried both a normal function and a member, and both result in the same error.
Here's the code that concerns this problem(it's scattered around a few files, but that's unimportant I think all are linked by inclusion):
PHP Code:
typedef LRESULT(*grCallbackWndProc)(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK MessageHandler(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam);
grErrorCode grWindow::Create(HINSTANCE hInstance,
grCallbackWndProc WndProc,
LPSTR sTitle,
int iWidth,
int iHeight,
int iStyle) {
if (0 == WndProc) {
//This line gives the error.
mWndProc = &MessageHandler;
} else {
mWndProc = WndProc;
}
//Rest of code that compiles fine.
}
This is using the GCC compiler.
Thanks for any insight.
Re: Callback Type Problem
To save you a serious headache, you cannot use a non-static member function as the window procedure, because of the hidden "this" pointer that needs to be passed to each member function.
Otherwise, if you're not using a member function, your code looks fine. Maybe try adding CALLBACK after LRESULT in your typdef -- CALLBACK is defined as __stdcall while the default calling convention is 'cdecl.' This might be the problem.
edit: I think you put the calling convention inside the parentheses -- ie,
typedef LRESULT( CALLBACK *grCallbackWndProc)(HWND, UINT, WPARAM, LPARAM);
but I'm not sure ;)
Re: Callback Type Problem
sunburnt's answer is correct.
But also, there's no need for the typedef. windows.h already contains one by the name of WNDPROC.
Re: Callback Type Problem
Thanks a lot. Works like a charm.