windows 0x00007FF8956DA430处出现未处理的异常(user32.dll)

pu82cl6c  于 2022-11-18  发布在  Windows
关注(0)|答案(1)|浏览(259)

我在尝试加载我的Window应用程序时收到一个错误,该错误称为“Tennis.exe中0x 00007 FF 8956 DA 430(user32.dll)处的未处理异常:数据类型:阅读位置”时发生访问冲突
windows . cpp-

Windows32::Windows32()
{
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)

{
    switch (msg)
    {
    case WM_CREATE:
    {   Windows32* window = (Windows32*)((LPCREATESTRUCT)lparam)->lpCreateParams;
        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)window);
        window->onCreate();
        break;
       // case WM_CREATE;
    }

    case WM_DESTROY:
    {   
        Windows32* window = (Windows32*)GetWindowLong(hwnd, GWLP_USERDATA);
        window->onDestroy();
        ::PostQuitMessage(0);
        break;
    }

    default:
        return ::DefWindowProc(hwnd, msg, wparam, lparam);

    }

    return NULL;
}


bool Windows32::init()
{
    WNDCLASSEX wc;
    wc.cbClsExtra = NULL;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbWndExtra = NULL;
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance = NULL;
    wc.lpszClassName = L"MyWindows32Class";
    wc.lpszClassName = L"";
    wc.style = NULL;
    wc.lpfnWndProc = &WndProc;

    if (!::RegisterClassEx(&wc))
        return false;

   // if (!window)
     //   window = this;

    m_hwnd=::CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, L"MyWindows32Class", L"DirectX Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768,
        NULL, NULL, NULL, this);

  
    if (!m_hwnd)
        return false;

    ::ShowWindow(m_hwnd, SW_SHOW);
    ::UpdateWindow(m_hwnd);

    //::RegisterClassEx(&wc);
 

  
    
    m_is_run = true;
    return true;
}

bool Windows32::broadcast()
{
    MSG msg;

    this->onUpdate();

    while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    Sleep(1);
    return true;
}

bool Windows32::release()
{
    if (!::DestroyWindow(m_hwnd))
        return false;
    return true;
}

bool Windows32::isRun()
{
    return m_is_run;
}

void Windows32::onCreate() {

}

void Windows32::onUpdate() {

}
void Windows32::onDestroy()
{
    m_is_run = false;
}

Windows32::~Windows32() {

}

Windows.h

#pragma once
#include <Windows.h>
class Windows32 
{
public:
    Windows32();
    bool init();
    bool broadcast();
    bool release();
    bool isRun();

    virtual void onCreate();
    virtual void onUpdate();
    virtual void onDestroy();



    ~Windows32();
protected:
    HWND m_hwnd;
    bool m_is_run;
};

Main.cpp

#include "Windows32App.h"

int main() {
    Windows32App app;
    if (app.init())
    {
        while (app.isRun())
        {
            app.broadcast();
        }
    }
    
    return 0;
}

WindowsApp.h

#pragma once
#include "Windows32.h"

class Windows32App: public Windows32
{
public:
   Windows32App();
   ~Windows32App();


// Inherited via Windows32
virtual void onCreate() override;
virtual void onUpdate() override;
virtual void onDestroy() override;

};
Windows32App.cpp

#include "Windows32App.h"


Windows32App::Windows32App()
{
}

Windows32App::~Windows32App()
{
}

void Windows32App::onCreate()
{
Windows32::onCreate();
}

void Windows32App::onUpdate()
{
Windows32::onUpdate();
}

void Windows32App::onDestroy()
{
Windows32::onDestroy();
}

我试过在互联网上搜索并寻找答案,我也试过调试器

vkc1a9a2

vkc1a9a21#

你必须实现从HWND创建Window32的构造函数。如果你只是简单地将HWND转换为Window32*,那么它可能会导致segfault,因为你在hwnd所指向的区域之外读写内存。换句话说,你的转换不会自动从HWND创建Window32。下面是我的建议:

public:
    Windows32(HWND hWnd)
    {
        this->m_hwnd = hWnd;
        this->m_is_run = true;
    }

用途

Windows32 window(((LPCREATESTRUCT)lparam)->lpCreateParams);

取而代之

Windows32* window = (Windows32*)((LPCREATESTRUCT)lparam)->lpCreateParams;

相关问题