11
votes

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'

3
What line are you getting that error on? I don't think it's in what you posted.Mat
See Raymond Chens scratch program for the idiomatic way of getting window messages passed to a class method (To answer the next logical question after static is added to the method signature).user786653

3 Answers

15
votes

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.

4
votes

That's because your WndProcedure function must be either a global function or a static member function.

4
votes

You can't use a non-static member function as a window procedure. If you declare WndProcedure as static it should compile. A non-member function would work as well.

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