RegisterClassEx fails

I'm setting up a window class and registering, like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
WNDCLASSEX wcBigWnd;
LRESULT CALLBACK mainWindow (HWND,UINT,WPARAM,LPARAM);

int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow) {

HICON hIcon=NULL;

wcBigWnd.cbSize=sizeof(WNDCLASSEX);
wcBigWnd.style=0;
wcBigWnd.lpfnWndProc=mainWindow;
wcBigWnd.cbClsExtra=0;
wcBigWnd.cbWndExtra=0;
wcBigWnd.hInstance=hInstance;
wcBigWnd.hIcon=hIcon;
wcBigWnd.hCursor=NULL;
wcBigWnd.hbrBackground=(HBRUSH)COLOR_BACKGROUND;
wcBigWnd.lpszMenuName=NULL;
wcBigWnd.lpszClassName="big_window_class";
wcBigWnd.hIconSm=NULL;

std::cout << "RegisterClassEx() returns: " << RegisterClassEx(&wcBigWnd) << "\n";

std::cout << "GetLastError() returns: " << GetLastError() << "\n";

}


and it gives me the output:
RegisterClassEx() returns: 49666
GetLastError() returns: 14007


On msdn (http://msdn.microsoft.com/en-us/library/windows/desktop/ms681384%28v=vs.85%29.aspx), it says:
ERROR_SXS_KEY_NOT_FOUND

14007 (0x36B7)

The requested lookup key was not found in any active activation context.



What does this mean and how can I fix it?

EDIT: when I use this class in CreateWindowEx("big_window_class",...), it returns NULL but GetLastError() returns 0
Last edited on
Is there code I'm not seeing? I see a function prototype ...

 
LRESULT CALLBACK mainWindow (HWND,UINT,WPARAM,LPARAM);


... for a mainWindow function, but I see no mainWindow function?

iostream will do you no good. If you want to know what a Windows program looks like, take a look at this minimal one. Take especial note of the fact that while I have no prototype for a Window Procedure, I do indeed have one, i.e., fnWndProc. And the address of that existing function is passed into Windows through the lpfnWndProc member of WNDCLASSEX::lpfnWndProc. The below code will only compile as an ansi build...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//Compiles to about 7K
#include <windows.h>

LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam,LPARAM lParam)
{
 if(msg==WM_DESTROY)
 {
    PostQuitMessage(0);
    return 0;
 }

 return (DefWindowProc(hwnd, msg, wParam, lParam));
}


int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 char szClassName[]="Form1";
 WNDCLASSEX wc={};
 MSG messages;
 HWND hWnd;

 wc.lpszClassName   = szClassName;
 wc.lpfnWndProc     = fnWndProc;
 wc.cbSize          = sizeof (WNDCLASSEX);
 wc.hInstance       = hIns;
 RegisterClassEx(&wc);
 hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,100,100,350,300,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
 }

 return messages.wParam;
}              


Also, don't use global data. Its no good. Put your WNDCLASSEX object declaration in WinMain(), not in your program's data segment.
Last edited on
The result of RegisterClassEx is an ATOM which is a numeric ID of the class.
If it fails, it returns 0.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633587(v=vs.85).aspx
Last edited on
@freddie1: that function prototype is just for the window procedure

I think the problem is line 16, where it gives a handle to something that isn't a brush - this would explain the error (the resource given in (HBRUSH)COLOR_BACKGROUND doesn't exist)
Last edited on
That was the problem after all. Thanks people for your input!
Topic archived. No new replies allowed.