在学习Windows开发期间,遇到的问题。
写了一个GUI项目,运行之后,没有反应。调试后发现,CreateWindow函数返回NULL。大概的代码如下:
#include "main.h"#include<algorithm>#include<iostream>char szFilePath[MAX_PATH] = { 0 };char szResult[4096];CQueue<QueueElement> queue;TCHAR* szMutex = _T("__mutex__l32__");int WINAPI _tWinMain(HINSTANCE hThis, HINSTANCE hPrev, LPTSTR szCommandLine, int iCmdShow){UNREFERENCED_PARAMETER(hPrev);UNREFERENCED_PARAMETER(szCommandLine);TCHAR* szWindowClass = _T("__concurrent_operation__");WNDCLASSEX wndEx;wndEx.cbSize = sizeof(WNDCLASSEX);wndEx.style = CS_HREDRAW | CS_VREDRAW;wndEx.lpfnWndProc = WindowProcedure;wndEx.cbClsExtra = 0;wndEx.cbWndExtra = 0;wndEx.hInstance = hThis;wndEx.hIcon = LoadIcon(wndEx.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));wndEx.hIconSm = LoadIcon(wndEx.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));wndEx.hCursor = LoadCursor(nullptr, IDC_ARROW);wndEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);wndEx.lpszMenuName = NULL;wndEx.lpszClassName = _T("__concurrent_operation__");if (!RegisterClassEx(&wndEx)){return 1;}InitCommonControls();HWND hWnd = CreateWindow(szWindowClass, _T("Concurrent operation"),WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, 50, 50, 305, 250,NULL, NULL, wndEx.hInstance, NULL);if (!hWnd){return 1;}ShowWindow(hWnd, SW_SHOW);HANDLE hMutex = CreateMutex(NULL, FALSE, szMutex);MSG msg;while (GetMessage(&msg, 0, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}CloseHandle(hMutex);UnregisterClass(wndEx.lpszClassName, wndEx.hInstance);return (int)msg.wParam;}
经过查找网络,终于发现 问题在于
wndEx.lpszClassName = _T("__concurrent_operation__");
和
CreateWindow(szWindowClass, _T("Concurrent operation"),
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, 50, 50, 305, 250,
NULL, NULL, wndEx.hInstance, NULL);
中的第一个参数不一致。虽然都是同一个字符串,但这样也不行。
后来改为 wndEx.lpszClassName = szWindowClass; 顺利通过