Skip to main content
GameDev.net gamedev.net
🔒 Locked

WNDPROC cast error

Started by JasonL220 Dec 26, 2006 at 3:02 PM 1 replies 1.5k views
Original Post
JasonL220
JasonL220
i have this line of code, i'm trying to create a general class ( calles Framework ) to construct a win32 window. wc.lpfnWndProc = &Framework::WndProc; but i get this casting error how do i correct it. error C2440: 'type cast' : cannot convert from 'LRESULT (__stdcall Framework::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC' There is no context in which this conversion is possible thanks
raz0r
raz0r
Make WndProc static.
LessBread
LessBread
See Creating a Win32 Window Wrapper Class for details on one approach to creating a window class. Here's another approach: WNDPROC Thunks.

The trouble is this.

wc.lpfnWndProc = &Framework::WndProc;

A WNDPROC type of function pointer is expected.

typedef LRESULT (CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);

where CALLBACK indicates the calling convention used. It's a macro defined as

#define CALLBACK __stdcall

Framework::WndProc - as a member function uses the __thiscall calling convention (See Member Function Pointers for details).

If typedefed as a __stdcall it would look like this (not exactly but close enough)

typedef LRESULT (CALLBACK* Framework_WNDPROC)(Framework* this, HWND, UINT, WPARAM, LPARAM);

The signatures of the two types of function pointers don't match. The method form expects it's first argument to be a pointer to the object, but the window passes a HWND as the first argument. That creates a problem down the road. Declaring Framework::WndProc as static resolves this conflict because in effect it turns the object method into an ordinary stand alone function that internally references the object as a static variable.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.