C++, WIN32, WndProc to Member Function Crash (Access violation at 0x88)

0

I've been writing a win32 wrapper class, and I've come across a problem: Because each instance of the class has a window, I've enclosed the this pointer in the user info space using SetWindowLongPtrW(), allowing me to call a message handler from the static WndProc function. This works fine: I can call the function. However, when I try to call another member function from the message handler, I get an access violation at 0x00000088 It does compile. I posted quite a lot, because to be honest I'm not too sure where the problem originates from... Please feel free to comment/criticize my code in general. Thanks for the help!

Here is the header:

#pragma once
#include <Windows.h>
#include "GlobalDefines.h"
#include "GraphicsWrapper.h"
#include "Keyboard.h"

namespace Startup
{

class GraphicsWrapper;

class WindowsWrapper
{
public:
    WindowsWrapper(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   INT nCmdShow);
    ~WindowsWrapper();
    void EnterMsgLoop(GraphicsWrapper* Gfx);
    static LRESULT _stdcall WindowProc(HWND hWnd,
                                       UINT message,
                                       WPARAM wParam,
                                       LPARAM lParam);
    LRESULT _stdcall MessageHandler(HWND hWnd,
                                    UINT message,
                                    WPARAM wParam,
                                    LPARAM lParam);
    WNDCLASSEX WndClass;
    MSG Message;
    RECT Desktop;
    RECT Taskbar;
    RECT WindowCoordinates;
    LPSTR CommandLineArgs;
    INT CmdShow;
    HINSTANCE TheInstance;
    HWND WindowHandle;

    void InitializeWndClassEx();
    void InitializeWindowHandleHWND();
    void ShowWindowOnScreen();
    bool GetScreenRect(RECT & Desktop);
    bool GetTaskbarRect(RECT& rectTaskbar);
    bool GetWindowCoords(RECT& WindowCoordinates);
    int GetTaskbarSide();
    enum TaskbarSides
    {
        Top,
        Right,
        Bottom,
        Left
    };
    void SetFullScreen(bool Enable);
};

static IO::Keyboard * kbd;
}

And this is the relevant part of the implementation. I'll mark where the crash occurs.

void Startup::WindowsWrapper::InitializeWndClassEx()
{
    WndClass.hIcon = LoadIcon(TheInstance,(MAKEINTRESOURCE(IDI_MAIN_ICON) ) );
    WndClass.hIconSm = LoadIcon(TheInstance,(MAKEINTRESOURCE(IDI_MAIN_ICON) ) );
    WndClass.cbSize = sizeof(WNDCLASSEX);
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    WndClass.lpfnWndProc = WindowProc;
    WndClass.hInstance = TheInstance;
    WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClass.lpszClassName = L"WindowClassName";
    RegisterClassEx(&WndClass);
    SetWindowLongPtrW(WindowHandle, GWLP_USERDATA, (long)this);
}

void Startup::WindowsWrapper::SetFullScreen(bool Enable)
{

    long style = Enable ? WS_POPUP : WS_OVERLAPPED | WS_SYSMENU;
    static RECT windowRect = {};
    static bool needRect = true;

    if (needRect)
    {
        GetWindowRect(WindowHandle, &windowRect);
        needRect = false;
    }

    SetWindowLong(WindowHandle, GWL_STYLE, style);

    if (Enable)
    {
        SetWindowPos(WindowHandle, HWND_TOPMOST,
                     0,0,
                     GetSystemMetrics(SM_CXSCREEN),
                     GetSystemMetrics(SM_CYSCREEN),
                     SWP_SHOWWINDOW);
    }
    else
    {
        SetWindowPos(WindowHandle, 0,
                     windowRect.left,windowRect.top,
                     windowRect.right - windowRect.left,
                     windowRect.bottom - windowRect.top,
                     SWP_SHOWWINDOW);
    }
}

and

LRESULT CALLBACK Startup::WindowsWrapper::WindowProc
(
    HWND hWnd,
    UINT message,
    WPARAM wParam,
    LPARAM lParam
)
{
    WindowsWrapper* ourObjectPtr = NULL;
    long thisObject = GetWindowLongW(hWnd, GWLP_USERDATA);

    ourObjectPtr = (WindowsWrapper *)( (void*)thisObject);

    long Result = ourObjectPtr->MessageHandler(hWnd, message, wParam, lParam);
    RET(Result);

}

LRESULT _stdcall Startup::WindowsWrapper::MessageHandler
(
    HWND hWnd,
    UINT message,
    WPARAM wParam,
    LPARAM lParam
)
{
    switch(message)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_KEYDOWN:
            switch(wParam)
        {
            case VK_ESCAPE:
                PostQuitMessage(0); //Works fine here, but...
                break;
            case VK_SPACE:
                this->SetFullScreen(false); //Crashes here w/ access violation
                break;
            case VK_SHIFT:
                this->SetFullScreen(true); //Or here, w/ the same error.
                break;
        }           
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

This is the createWindowEx call. Thanks for your help, again.

void Startup::WindowsWrapper::InitializeWindowHandleHWND()
{
WindowHandle = CreateWindowEx(NULL,
    L"WindowClassName",
            L"WindowTitle"
    WS_OVERLAPPED | WS_SYSMENU,
    WindowCoordinates.left, WindowCoordinates.top,
    WindowCoordinates.right, WindowCoordinates.bottom,
    NULL, NULL, TheInstance,
    CommandLineArgs);
}
c++
windows
winapi
visual-c++
asked on Stack Overflow Mar 18, 2013 by smiling_nameless • edited Mar 18, 2013 by smiling_nameless

1 Answer

1

I have some code from a custom dialog handler I wrote quite a while back, which might be of use to you.

Same principle applies for a window but switch the WM_INITDIALOG for WM_CREATE and also replace DWLP_USER with GWLP_USERDATA. The format of the callback is subtley different too. You should be able to salvage almost all of this function though.

LRESULT CALLBACK CDialog::DlgProc( HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
    CDialog* pWindow;

    if( msg == WM_INITDIALOG ) {
        SetWindowLongPtr( hWndDlg, DWLP_USER, (LONG_PTR)lParam );
        pWindow = reinterpret_cast<CDialog*>( lParam );
        pWindow->m_hWnd = hWndDlg;
    } else {
        pWindow = reinterpret_cast<CDialog*>( (LPARAM)GetWindowLongPtr( hWndDlg, DWLP_USER ) );
    }

    if( pWindow != NULL ) {
        LRESULT ret = pWindow->OnMessage( msg, wParam, lParam );
        if( msg == WM_NCDESTROY ) pWindow->m_hWnd = NULL;
    }

    return FALSE;
}
answered on Stack Overflow Mar 18, 2013 by paddy

User contributions licensed under CC BY-SA 3.0