簡體   English   中英

0x00007FF8956DA430 (user32.dll) 處的未處理異常

[英]Unhandled exception at 0x00007FF8956DA430 (user32.dll)

嘗試加載我的 Window 應用程序時出現錯誤,該錯誤稱為“Tennis.exe 中 0x00007FF8956DA430 (user32.dll) 處未處理的異常:0xC0000005:訪問沖突讀取位置”

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

主.cpp

#include "Windows32App.h"

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

Windows應用程序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();
}

我試過在 inte.net 上搜索並尋找答案,我試過調試器

您必須實現從HWND創建Window32的構造函數。 如果您只是將HWND轉換為Window32* ,那么它可能會導致段錯誤,因為您在hwnd指向的區域邊界之外寫入和讀取 memory。 換句話說,您的轉換不會自動從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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM