C++ – C2440 Can’t convert LRESULT to WNDPROC in C++ WinApi

cwinapiwindows

I'm trying to write this win32 program with WinApi and I'm stuck because the tutorial I'm following seems to have a problem.

MainWindow.h:

class MainWindow
{
  public:
    MainWindow(HINSTANCE);
   ~MainWindow(void);

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

    // [...]

MainWindow.cpp:

MainWindow::MainWindow(HINSTANCE hInstance) : hwnd(0)
{
  WNDCLASSEX WndClsEx;
  // [...]
  WndClsEx.lpfnWndProc = &MainWindow::WndProcedure;
  // [...]
}

LRESULT CALLBACK MainWindow::WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
  // [...]
}

I must be referencing MainWindow::WndProcedure wrong because I'm following the signature exactly as the tutorial says, however the lpfnWndProc line in the constructor gives a compile-time error:

error C2440: '=' : cannot convert from 'LRESULT (__stdcall MainWindow::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC'

Best Answer

replace

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

by

static LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);

The this pointer is a hidden parameter in your function call and by declaring it static the this pointer is not a parameter anymore and the signature of the two functions match.

Related Topic