0

I have a Window class which contains a method to set the attribute for the window handle (HWND). The method executes the following function:

_hWnd = CreateWindowEx(dwExStyle, _wcex.lpszClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, _hInstance, this);

I am passing the this parameter because I'm using a static method as a wrapper around the WndProc() function, which will then redirect the call to (non-static) method. The call to CreateWindowEx() should put the pointer in a struct and pass me the memory address of that struct back when it calls WndProc(). But when trying to recover the object pointer from the lParam parameter, I cannot seem to recover the pointer to my object, as if the value that the windows API passes to lParam is wrong.

I have uploaded the full code now:

#include <Windows.h>
#include "Window.h"

LRESULT CALLBACK WndProc_main(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM);

int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE prevInstance, _In_opt_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
    const WCHAR szClassName_main[] = L"Main Window Class";

    WNDCLASSEX wcex_main;
    wcex_main.cbSize = sizeof(WNDCLASSEX);
    wcex_main.style = CS_HREDRAW | CS_VREDRAW;
    wcex_main.lpfnWndProc = NULL;
    wcex_main.cbClsExtra = 0;
    wcex_main.cbWndExtra = 0;
    wcex_main.hInstance = hInstance;
    wcex_main.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcex_main.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex_main.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);   // MSDN: +1 should be added to chosen color.
    wcex_main.lpszMenuName = NULL;
    wcex_main.lpszClassName = szClassName_main;
    wcex_main.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    Window window_main = Window(hInstance, wcex_main, WndProc_main);

    window_main.setWindowHandle(WS_EX_CLIENTEDGE, L"Main Window Title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, NULL);
    window_main.DisplayWindow(nCmdShow);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return static_cast<int> (msg.wParam);
}

LRESULT CALLBACK WndProc_main(_In_ HWND hWnd, _In_ UINT msg, _In_ WPARAM wParam, _In_ LPARAM lParam)
{
    switch (msg)
    {
    case WM_CLOSE:
        DestroyWindow(hWnd);
    case WM_DESTROY:
        PostQuitMessage(EXIT_SUCCESS);
    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return EXIT_SUCCESS;
}

Window.h

#ifndef WINDOW_H
#define WINDOW_H

#include <Windows.h>

class Window
{
    public:
        // Constructors
        Window(_In_ HINSTANCE, _In_ WNDCLASSEX, _In_ WNDPROC);
        int setWindowHandle(_In_ DWORD dwExStyle, _In_opt_ LPCWSTR lpWindowName,_In_ DWORD dwStyle, _In_ int x,
                            _In_ int y, _In_ int nWidth, _In_ int nHeight,_In_opt_ HWND hWndParent,
                            _In_opt_ HMENU hMenu, _In_opt_ LPVOID lpParam);
        void DisplayWindow(_In_ int nCmdShow);;

        // Destructor
        ~Window();

        // Public methods
        int registerWindowClass();
        int bindProcFunc(_In_ WNDPROC);
        static LRESULT CALLBACK WndProcWrapper(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM);

    protected:
    private:
        HINSTANCE _hInstance;
        WNDCLASSEX _wcex;
        WNDPROC _WndProc;
        HWND _hWnd;
};

#endif // WINDOW_H

Window.cpp

#include "Window.h"


Window::Window(HINSTANCE hInstance, WNDCLASSEX wcex, WNDPROC WndProc)
{
    _hInstance = hInstance;
    _wcex = wcex;
    _wcex.lpfnWndProc = WndProcWrapper;
    registerWindowClass();
    bindProcFunc(WndProc);
}

Window::~Window()
{
    // destructor
}

int Window::registerWindowClass()
{
    if (!RegisterClassEx(&_wcex))
    {
        DWORD error = GetLastError();
        if (error == ERROR_CLASS_ALREADY_EXISTS)
        {
            return EXIT_SUCCESS;
        }
        else
        {
            MessageBox(NULL, L"Window class registration failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
            return EXIT_FAILURE;
        }
    }
    return EXIT_SUCCESS;
}

int Window::bindProcFunc(WNDPROC WndProc)
{
    _WndProc = WndProc;
    return EXIT_SUCCESS;
}

LRESULT CALLBACK Window::WndProcWrapper(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    Window* pThis;
    if (msg = WM_NCCREATE)
    {
        pThis = static_cast<Window*> ((reinterpret_cast<CREATESTRUCT*>(lParam))->lpCreateParams);
        SetLastError(0);
        if (!SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis)))
        {
            if (GetLastError() != 0)
            {
                MessageBox(NULL, L"You messed up.", L"Error!", MB_ICONEXCLAMATION | MB_OK);
                return FALSE;
            }
        }
    }
    else
    {
        pThis = reinterpret_cast<Window*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
    }

    if(pThis)
    {
        return pThis->_WndProc(hWnd, msg, wParam, lParam);
    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
}

int Window::setWindowHandle(_In_ DWORD dwExStyle, _In_opt_ LPCWSTR lpWindowName,_In_ DWORD dwStyle, _In_ int x,
                            _In_ int y, _In_ int nWidth, _In_ int nHeight, _In_opt_ HWND hWndParent,
                            _In_opt_ HMENU hMenu, _In_opt_ LPVOID lpParam)
{       
    _hWnd = CreateWindowEx(dwExStyle, _wcex.lpszClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, _hInstance, this);

    if(_hWnd == NULL)
    {
        MessageBox(NULL, L"Window Handle creation failed", L"Error!", MB_ICONEXCLAMATION | MB_OK);
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

void Window::DisplayWindow(_In_ int nCmdShow)
{
    ShowWindow(_hWnd, nCmdShow);
    UpdateWindow(_hWnd);
}

I found this code on here https://stackoverflow.com/questions/21369256/how-to-use-wndproc-as-a-class-function and on a few other sites, and I checked the input parameters to CreateWindowEx(), and I just can't seem to find where it goes wrong. The code compiles without any warnings. Can anyone help me? I'm using Visual Studio 2013, compiling for 64 bit.

P.S: I also tried replacing WM_NCCREATE with WM_CREATE, but that doesn't seem to help.

4
  • Is the window created using a dialog template?
    – Jonathan
    Commented Feb 3, 2016 at 13:59
  • Your idea is right. Static method of a class can be used as a window function. I used that for decades. Your code is incomplete. The problem is else where. Check creation of window class and use debugger. Commented Feb 3, 2016 at 14:04
  • EDIT: I have uploaded the full code now. Commented Feb 3, 2016 at 14:39
  • @Hans I am defining the WndProc() functions that I am going to use elsewhere, so I can bind them to the _wndProc attribute of the object for different types of windows instead of hardcoding it into the class. The issue is not related to WndProc_main() though because I cannot even access an attribute of my object using pThis->, meaning that the pointer is wrong. Commented Feb 3, 2016 at 15:36

1 Answer 1

3

I got interested in this problem, spent some time debugging it, placing special marker into that CREATESTRUCT and looking for them in the memory window, etc.

Then I got lucky: while I let it run, I noticed that WM_NCCREATE case was entered repeatedly, and taking a closer look I got it:

if (msg = WM_NCCREATE)

That would grab the first message (WM_GETMINMAXINFO), do some inappropriate casting, and so on...

6
  • That is probably the error hah; I didnt see but I forgot an "=" sign there. Yup, that fixed it. Commented Feb 3, 2016 at 15:54
  • Yeah because I assumed that WM_NCCREATE was the first message (bad assumption!) and because it entered the if statement like I expected I never really thought of checking the message. Commented Feb 3, 2016 at 15:58
  • 3
    @Nicolai Microsoft's compiler will issue a warning about this (C4706), assuming you're compiling at warning level 4 (/W4) or higher. You should be, would have saved you and Vlad a bunch of time. :-)
    – Cody Gray
    Commented Feb 4, 2016 at 4:12
  • 2
    Also, on a more pedantic note, I feel compelled to point out that WM_GETMINMAXINFO is not necessarily the first message a new window receives. In fact, it is best not to assume anything about the order of messages, that's why the code checks specifically for WM_NCCREATE. You are guaranteed to get that one (along with WM_NCCALCSIZE and WM_CREATE), but you aren't guaranteed to get the messages in any order. Child windows won't get WM_GETMINAMXINFO at all. Again, not criticizing the code here, just the implicit assumption that WM_GETMINMAXINFO will always be the first message.
    – Cody Gray
    Commented Feb 4, 2016 at 4:17
  • @Cody yeah after I fixed the bug I was wondering if I could ask Microsoft to implement such a feature to give warnings when doing assignments in if statements and then I found out about the warning level :) Commented Feb 4, 2016 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.