C2440 Unable to convert LRESULT to WNDPROC in C ++ WinApi

I am trying to write this win32 program using WinApi and I am stuck because I have a problem with the tutorial.

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 have to refer to MainWindow :: WndProcedure incorrectly because I follow the signature in exactly the same way as the tutorial says, however the line lpfnWndProc in the constructor gives a compile-time error:

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

+7
source share
3 answers

replace

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

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

This pointer is a hidden parameter in a function call and declaring it static, this pointer is no longer a parameter, and the signature of the two functions matches.

+12
source

You cannot use a non-stationary member function as a window procedure. If you declare WndProcedure as static , it must compile. A non-member function will also work.

Non-static member functions have a different signature than static members. This is because they get an implicit this parameter in addition to explicitly defined parameters.

+3
source

This is because your WndProcedure function must be either global or static .

+3
source

All Articles