Click to See Complete Forum and Search --> : WndProc Type Cast Error?


Speedi3579
November 7th, 2003, 03:46 PM
Hello.

I have made a class that handles creating and registering the window. My WndProc function is one of the public functions of this class. It's protype looks like:

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

and the definition:
LRESULT CALLBACK awWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
...
code
...
}

awWindow is the class it is in.

Now the error comes up on this line of code, which is in my CreateWindow function, whish is a member of awWindow class.

wc.lpfnWndProc = (WNDPROC) WndProc;

The error code is:

error C2440: 'type cast' : cannot convert from ' ' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'
None of the functions with this name in scope match the target type

Does anyone know how I can get this solved? What have I done wrong?

Paul McKenzie
November 7th, 2003, 04:33 PM
The WndProc must either be a global function or static function -- it cannot be a non-static class member function. The reason why is that a non-static class member function is not compatible with a normal global or static member function due to the this pointer (these are the rules of C++).

There are probably hundreds of messages in CodeGuru that explain this, and workarounds to this problem. One workaround is to create a global map of HWND's to window objects. When the global (or static) WndProc is called, you look into the map for the window object that matches the HWND, and then you call your window object's procedure function (which can now be a non-static member).

Regards,

Paul McKenzie

Andreas Masur
November 7th, 2003, 05:37 PM
Take a look at the following FAQ (http://www.codeguru.com/forum/showthread.php?s=&threadid=231047)...