c++, WIN32, WndProc到成员函数崩溃(访问冲突0x88)



我一直在编写一个win32包装器类,并且我遇到了一个问题:因为类的每个实例都有一个窗口,我使用SetWindowLongPtrW()this指针封闭在用户信息空间中,允许我从静态WndProc函数调用消息处理程序。这很好:我可以调用这个函数。但是,当我尝试从消息处理程序调用另一个成员函数时,我在0x00000088处获得访问冲突它确实可以编译。我贴了很多,因为说实话,我不太确定问题是从哪里来的……请随意评论/批评我的代码。谢谢你的帮助!

标题:

#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;
}

这是实现的相关部分。我将标记崩溃发生的位置。

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);
    }
}

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);
}

这是createWindowEx呼叫。再次感谢您的帮助。

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);
}

我有一些自定义对话处理程序的代码,我写了很长一段时间,这可能对你有用。

同样的原则适用于窗口,但将WM_INITDIALOG切换为WM_CREATE,并将DWLP_USER替换为GWLP_USERDATA。回调的格式也有细微的不同。但是,您应该能够挽救几乎所有的这个函数。

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;
}

最新更新