Error 1813 when calling CreateWindow () func WinApi

I am new to C ++ and WinApi. I am unable to create a simple window in WinApi. The CreateWindow () function returns null. GetLastError () function func returns error 1813. But before the window is created GetLastError () returns 0. Sorry for my english. Here is my complete code:

#include <Windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
 LPCWSTR szWindowClass = TEXT("WndClass");
 LPCWSTR szTitle = TEXT("Main window");
 DWORD dwError;

 WNDCLASS wc;
 wc.style = CS_OWNDC;
 wc.hInstance = hInstance;
 wc.cbClsExtra = 0;
 wc.cbWndExtra = 0;
 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
 wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);

 wc.lpfnWndProc = WndProc;
 wc.lpszClassName = szWindowClass;
 wc.lpszMenuName = L"MenuName";
 dwError = GetLastError(); //0

 RegisterClass(&wc);
 dwError = GetLastError();//0


 HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);//NULL

 dwError = GetLastError();//1813 =(
 return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
 return 0;
}

      

0


source to share


2 answers


First of all, your error handling is wrong. The documentation states that you GetLastError

only call on failure CreateWindow

. And failure is CreateWindow

indicated by the return value NULL

. You should check the return value CreateWindow

before calling GetLastError

. Please read the documentation carefully .

You are making the same mistake when calling RegisterClass

. In your defense, this is the most common mistake made by novice Win32 programmers.

Error code 1813 ERROR_RESOURCE_TYPE_NOT_FOUND

. The documentation says:

The specified resource type could not be found in the image file.

Again, you can find out this information by reading the documentation as soon as you know where to look.



This means it is CreateWindow

trying to find a resource that is not in the file. You may have been unable to link the menu resource.

Your window procedure is also damaged. It should be:

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc(hWnd, Msg, wParam, lParam);
}

      

When you start adding individual message handling for specific messages, make sure you still call DefWindowProc

for any other messages.

+1


source


You need to return a result DefWindowProc

for messages that you are not handling yourself.
See here for details .



+2


source







All Articles