Code:

int Window::Create() { WNDCLASSEX wcex{}; auto szWindowClass = _T("Scream2DEngineWindow"); auto hInstance = GetModuleHandle(nullptr); auto hIcon = LoadIcon(nullptr, IDI_APPLICATION); wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_DBLCLKS; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = hIcon; wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wcex.lpszMenuName = nullptr; wcex.lpszClassName = szWindowClass; wcex.hIconSm = hIcon; if (!RegisterClassEx(&wcex)) { return 1; } auto hWnd = CreateWindowEx(0, szWindowClass, "Title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, HWND_DESKTOP, nullptr, hInstance, nullptr); std::cout << GetLastError() << std::endl; if (!hWnd) { return 2; } ShowWindow(hWnd, SW_SHOW); return 0; } 

It outputs “0” to the console (GetLastError), but the function returns 2, that is, hWnd == 0.

Where is the mistake?

  • Check the returned hWnd with IsWindow() . If the HWND is not null (and you yourself wrote that hWnd is two), it is quite possible that the system simply gives you such a descriptor, and as it should be. - ߊߚߤߘ
  • one
    @ ScreamTV5, before returning HWND from CreateWindowEx , WndProc will be called several times. Attach the contents of this function to the question; this may clarify the situation. - mega

1 answer 1

GetLastError should be called immediately.

 auto hWnd = CreateWindowEx(..); auto err = GetLastError(); std::cout << err << std::endl; 
  • It does not change anything as it was 0 and remained): - Andrey