Win32 CreateWindow() 错误,这是 nullptr
Win32 CreateWindow() error, this is nullptr
我正在另一个 class 中创建一个子 window,所以我将父级的 hWnd 和 hInstance 传递到函数中,我在其中创建子 window.
我现在的问题是,子 window 的 createWindow() 函数挂断了,我收到一条错误消息:"An exception has beend encountered. This may be caused by an extension".
有人知道这条消息是什么意思,或者我做错了什么吗?
在这里,我在父 window 的消息处理程序中调用子 window,因为我使用的是带有 ID 的子菜单。
LRESULT CALLBACK System::MessageHandler(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
MainMenu mMainMenu;
switch (message)
{
case WM_COMMAND:
{
switch (LOWORD(wparam))
{
//If user presses on the exit button
case IDM_FILE_EXIT:
{
PostQuitMessage(0);
} break;
case IDM_NEW_NEWPROJECT:
{
//////////////////////////////////////////////
// Here is the error showing up
//////////////////////////////////////////////
m_CreateProjectMenu->Initialize(m_hWnd, m_hinstance);
}break;
default:
break;
}
}
// Any other messages send to the default message handler as our application won't make use of them.
default:
{
return DefWindowProc(hwnd, message, wparam, lparam);
}
}
}
初始化:
bool CreateProjectMenu::Initialize(HWND m_ParentWindow, HINSTANCE m_hParentInstance)
{
//Initialize the window
InitializeWindow(m_ParentWindow, m_hParentInstance);
return true;
}
初始化窗口:
void CreateProjectMenu::InitializeWindow(HWND m_ParentWindow, HINSTANCE m_hParentInstance)
{
wc.style = CS_HREDRAW | CS_VREDRAW; // Defines additional elements of the window class.
wc.lpfnWndProc = ChildProc; // A pointer to the window procedure.
wc.cbClsExtra = 0; // The number of extra bytes to allocate following the window-class structure.
wc.cbWndExtra = 0; // The number of extra bytes to allocate following the window instance.
wc.hInstance = m_hParentInstance; // A handle to the instance that contains the window procedure.
wc.hIcon = LoadIcon(wc.hInstance, IDI_APPLICATION); // Load the icon for the application.
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load the cursor for the application.
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // Load the background for the application.
wc.lpszMenuName = NULL; // Pointer to a character string that specifies the name of the resource class menu.
wc.lpszClassName = m_ProjectMenuWindowName; // Set the name for the window.
wc.hInstance = m_hParentInstance;
if (!RegisterClass(&wc))
{
MessageBox(NULL, L"Failed to register the menuprojectwindow", L"Error", MB_OK);
}
m_NewProjectHwnd = CreateWindow(NULL,
m_ProjectMenuWindowName,
WS_CHILD | WS_VISIBLE | WS_CAPTION
| WS_SYSMENU | WS_THICKFRAME
| WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
screenWidth, screenHeight,
m_ParentWindow,
NULL,
m_hParentInstance,
NULL);
// Check if the hwnd is zero(error)
// Display a messagebox with a error
if (m_NewProjectHwnd == 0)
MessageBox(NULL, L"Could not create the create project hwnd.", L"Error", MB_OK);
else
{
ShowWindow(m_NewProjectHwnd, SW_SHOW); // Bring the window up on the screen
SetFocus(m_NewProjectHwnd);
}
return;
}
这是重现错误的代码:
https://ufile.io/ddmj4
this
是每个非静态 class 方法的隐式第一个参数。它是指向调用方法的对象的指针。您收到的错误消息意味着您已经在 nullptr
上调用了一个方法。尽管您的代码不完整,但唯一可能发生这种情况的行是
m_CreateProjectMenu->Initialize(m_hWnd, m_hinstance);
您可以通过在调用之前使用调试器检查 m_CreatePorjectMenu
的值,或添加 assert(CreatePorjectMenu);
来验证这一点。对于后者,请确保您在编译时启用了断言。
至于怎么解决,不了解你的项目结构就不好说了。某些函数必须负责初始化该对象,并且您必须确保在回调之前调用它。或者,如果初始化程序模式由于某种原因不起作用,您的回调可以检查 nullptr
并在必要时创建对象。
我正在另一个 class 中创建一个子 window,所以我将父级的 hWnd 和 hInstance 传递到函数中,我在其中创建子 window.
我现在的问题是,子 window 的 createWindow() 函数挂断了,我收到一条错误消息:"An exception has beend encountered. This may be caused by an extension".
有人知道这条消息是什么意思,或者我做错了什么吗?
在这里,我在父 window 的消息处理程序中调用子 window,因为我使用的是带有 ID 的子菜单。
LRESULT CALLBACK System::MessageHandler(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
MainMenu mMainMenu;
switch (message)
{
case WM_COMMAND:
{
switch (LOWORD(wparam))
{
//If user presses on the exit button
case IDM_FILE_EXIT:
{
PostQuitMessage(0);
} break;
case IDM_NEW_NEWPROJECT:
{
//////////////////////////////////////////////
// Here is the error showing up
//////////////////////////////////////////////
m_CreateProjectMenu->Initialize(m_hWnd, m_hinstance);
}break;
default:
break;
}
}
// Any other messages send to the default message handler as our application won't make use of them.
default:
{
return DefWindowProc(hwnd, message, wparam, lparam);
}
}
}
初始化:
bool CreateProjectMenu::Initialize(HWND m_ParentWindow, HINSTANCE m_hParentInstance)
{
//Initialize the window
InitializeWindow(m_ParentWindow, m_hParentInstance);
return true;
}
初始化窗口:
void CreateProjectMenu::InitializeWindow(HWND m_ParentWindow, HINSTANCE m_hParentInstance)
{
wc.style = CS_HREDRAW | CS_VREDRAW; // Defines additional elements of the window class.
wc.lpfnWndProc = ChildProc; // A pointer to the window procedure.
wc.cbClsExtra = 0; // The number of extra bytes to allocate following the window-class structure.
wc.cbWndExtra = 0; // The number of extra bytes to allocate following the window instance.
wc.hInstance = m_hParentInstance; // A handle to the instance that contains the window procedure.
wc.hIcon = LoadIcon(wc.hInstance, IDI_APPLICATION); // Load the icon for the application.
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load the cursor for the application.
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // Load the background for the application.
wc.lpszMenuName = NULL; // Pointer to a character string that specifies the name of the resource class menu.
wc.lpszClassName = m_ProjectMenuWindowName; // Set the name for the window.
wc.hInstance = m_hParentInstance;
if (!RegisterClass(&wc))
{
MessageBox(NULL, L"Failed to register the menuprojectwindow", L"Error", MB_OK);
}
m_NewProjectHwnd = CreateWindow(NULL,
m_ProjectMenuWindowName,
WS_CHILD | WS_VISIBLE | WS_CAPTION
| WS_SYSMENU | WS_THICKFRAME
| WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
screenWidth, screenHeight,
m_ParentWindow,
NULL,
m_hParentInstance,
NULL);
// Check if the hwnd is zero(error)
// Display a messagebox with a error
if (m_NewProjectHwnd == 0)
MessageBox(NULL, L"Could not create the create project hwnd.", L"Error", MB_OK);
else
{
ShowWindow(m_NewProjectHwnd, SW_SHOW); // Bring the window up on the screen
SetFocus(m_NewProjectHwnd);
}
return;
}
这是重现错误的代码: https://ufile.io/ddmj4
this
是每个非静态 class 方法的隐式第一个参数。它是指向调用方法的对象的指针。您收到的错误消息意味着您已经在 nullptr
上调用了一个方法。尽管您的代码不完整,但唯一可能发生这种情况的行是
m_CreateProjectMenu->Initialize(m_hWnd, m_hinstance);
您可以通过在调用之前使用调试器检查 m_CreatePorjectMenu
的值,或添加 assert(CreatePorjectMenu);
来验证这一点。对于后者,请确保您在编译时启用了断言。
至于怎么解决,不了解你的项目结构就不好说了。某些函数必须负责初始化该对象,并且您必须确保在回调之前调用它。或者,如果初始化程序模式由于某种原因不起作用,您的回调可以检查 nullptr
并在必要时创建对象。