Search code examples
c++winapiruntime-error

RegisterClass succeeds yet class does not exist


I have just recently began a project using the Windows API. I am well aware of how I am supposed to operate it, and already made another working project (which worked just fine). Now that I have copied the code of just mentioned project, it does not work

I'm talking about the class registration. Error handling is in place and GetLastError() gives me 0 (no error). The code currently looks like this:

WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_OWNDC;
wc.lpfnWndProc = WindowsProcedure;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = nullptr;
wc.hIconSm = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = className; // I am using Multi-Byte characters so no 
                              // problem here

// register the class
// this is a variable so I can the content of it in the debugger
// notice that this should be NULL if RegisterClassEx fails, it is not NULL however
DWORD a = RegisterClassEx(&wc); WIN_LAST_EXCEPT;

This code is part of a class inside of a function, className and hInst are members of it.

The WIN_LAST_EXCEPT macro you can see there contains this:

#define WIN_LAST_EXCEPT     {                           \
                   if (GetLastError() > 0)              \
                   {                            \
                      WinAPIException e(__LINE__, __FILE__);        \
                      throw e;                      \
                   }                            \
                } // put after function calls that make errors

I thought that maybe the end of the scope clears something up that it's not supposed to, though I don't see anything.

One important thing to note is that the HINSTANCE in an instance of the initialized class is null.

And lastly, I only noticed the entire situation because of a check that I have implemented right before a call to the CreateWindowEx() function.

It looks like this:

// check if the class is registered
WNDCLASSEX wc = { 0 };
GetClassInfoEx(wndClass.GetInstance(), wndClass.GetName(), &wc); WIN_LAST_EXCEPT;

Again, the error checking macro is here, this time however the GetLastError() function returns 1411, which according to MSDN stands for "Class does not exist".


Solution

  • Solved!

    I just went through the documentation and saw that the second Parameter of the ShowWindow function indicates the state of the window it is supposed to set it to. In my Code example you can see that I call

    ShowWindow(hWnd, 0)

    0 in this case means that the window is getting hidden, just set it to 1 and I can now see my window!

    I dont exactly know why the GetClassInfoEx function tells me that the class does not exist, but im guessing that the data is getting handled incorrectly in my get-function