Share via


WindowProc: cannot convert from 'LRESULT (__stdcall *)(HWND,UINT,LPARAM,WPARAM)' to 'WNDPROC'

Question

Tuesday, February 6, 2007 5:43 AM

Hello,

I am having a problem setting up a generic Win32 application. Specifically, when I'm filling out the WNDCLASSEX structure, the compiler shows an error when I try to assign the window procedure function to the lpfnWndProc member variable. Here is the relevant code:

WNDCLASSEX wcl;
wcl.cbSize = sizeof( WNDCLASSEX );
wcl.lpszClassName = g_szWinName;
wcl.hInstance = hInstance;
wcl.lpfnWndProc = WndProc;     // Error here
wcl.style = 0;
wcl.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wcl.hIconSm = LoadIcon( NULL, IDI_WINLOGO );
wcl.hCursor = LoadCursor( NULL, IDC_ARROW );
wcl.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wcl.lpszMenuName = NULL;
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;

The WndProc function was declared like this:
LRESULT CALLBACK WndProc( HWND, UINT, LPARAM, WPARAM );

The compiler error reads like this:
error C2440: '=' : cannot convert from 'LRESULT (__stdcall *)(HWND,UINT,LPARAM,WPARAM)' to 'WNDPROC'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast

The function declaration concurs with the MSDN documentation for WindowProc. I could not find a solution online, but I found something that could potentially be helpful, if only I knew how I could extract what's there and apply it to my situation:

What Can Go Wrong When You Mismatch The Calling Convention?

Can anyone advise me as to what is the correct method for declaring and using the window procedure function?

All replies (2)

Tuesday, February 6, 2007 7:32 AM âś…Answered

In my opinion, instead of

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

{

    . . .

}

you have to try

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

{

    . . .

}

I hope this helps.


Tuesday, February 6, 2007 9:24 PM

Thanks! That fixed it. Quite a silly mistake!