[英]Unhandled exception at 0x00007FF8956DA430 (user32.dll)
I get an error when trying to load my Window Application, the error is called "Unhandled exception at 0x00007FF8956DA430 (user32.dll) in Tennis.exe: 0xC0000005: Access violation reading location"尝试加载我的 Window 应用程序时出现错误,该错误称为“Tennis.exe 中 0x00007FF8956DA430 (user32.dll) 处未处理的异常:0xC0000005:访问冲突读取位置”
Windows.cpp - 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 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主.cpp
#include "Windows32App.h"
int main() {
Windows32App app;
if (app.init())
{
while (app.isRun())
{
app.broadcast();
}
}
return 0;
}
WindowsApp.h 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 Windows32App.cpp
#include "Windows32App.h"
Windows32App::Windows32App()
{
}
Windows32App::~Windows32App()
{
}
void Windows32App::onCreate()
{
Windows32::onCreate();
}
void Windows32App::onUpdate()
{
Windows32::onUpdate();
}
void Windows32App::onDestroy()
{
Windows32::onDestroy();
}
I have tried searching up on the inte.net and looking for answers and I have tried the debugger我试过在 inte.net 上搜索并寻找答案,我试过调试器
You have to implement constructor which creates Window32
from HWND
.您必须实现从HWND
创建Window32
的构造函数。 If you simply cast HWND
to Window32*
, then it could lead to segfault because you write to and read from memory that is outside the bounds of the area that hwnd
points to.如果您只是将HWND
转换为Window32*
,那么它可能会导致段错误,因为您在hwnd
指向的区域边界之外写入和读取 memory。 In other words, your cast will not automatically create Window32
from HWND
.换句话说,您的转换不会自动从HWND
创建Window32
。 Here is my suggestion:这是我的建议:
public:
Windows32(HWND hWnd)
{
this->m_hwnd = hWnd;
this->m_is_run = true;
}
Use利用
Windows32 window(((LPCREATESTRUCT)lparam)->lpCreateParams);
instead反而
Windows32* window = (Windows32*)((LPCREATESTRUCT)lparam)->lpCreateParams;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.