Search code examples
cwinapivisual-c++wrapperwndproc

How to wrap a win32 WndProc into a C++ class?


Is this even possible? For example, let's say I have the following:

class Window {
private:
    WNDCLASSEX wc;
public:
    inline WNDCLASSEX getWindowClass() {
        return wc;
    }
    Window();
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, LPARAM lParam, WPARAM wParam);
}

void RegisterWindow(Window win) {
    WNDCLASSEX* wc = win.getWindowClass();
    RegisterClassEx(wc);

}

Now, somewhere there is going to be a section (probably in the constructor of the Window class, where it's necessary to assign the WNDCLASSEX a WndProc, which is noted in the Window class. The only issue is that, because it's a part of a class, there an error will be raised. Thus, how is this achieved? Is it made static? Even so, if the class wraps it it is still part of the class in some way. If I create it outside of the class, that simply obliterates the point.


Solution

  • You pass the this pointer as GWLP_USERDATA to SetWindowLongPtr, which effectively allows you to simply forward the free function to the member function.