Window class not found - why?

This is a discussion on Window class not found - why? within the Windows Programming forums, part of the Platform Specific Boards category; Having adapted my program to using malloc instead of direct allocation, I have this strange, unexpected problem. Without even touching ...

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    Window class not found - why?

    Having adapted my program to using malloc instead of direct allocation, I have this strange, unexpected problem. Without even touching these Windows elements, suddenly, BitBlt stopped working (causing something to appear in the top left of my screen drawn on top of everything, making it impossible to see what I'm doing. I found the cause to be the WindowHandle being 0. Using common diagnostics, the "GetLastError" function, I see that the class is not being found. Why is this? How come, without even changing this content, it suddenly stopped working? The only change I made was that of using malloc (which works as far as I can tell) for my image data, which is irrelevent....

    Code:
    // Window definitions
    HWND WindowHandle; // window handle
    WNDCLASSEX WindowClass; // window class
    
    // in the WinMain function, first thing
    	WindowClass.cbSize = sizeof(WNDCLASSEX);
    	WindowClass.style = CS_HREDRAW | CS_VREDRAW;
    	WindowClass.lpfnWndProc = WndProc;
    	WindowClass.cbClsExtra = 0;
    	WindowClass.cbWndExtra = 0;
    	WindowClass.hInstance = hInstance;
    	WindowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    	WindowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    	WindowClass.lpszMenuName = NULL;
    	WindowClass.lpszClassName = "MyClass";
    	WindowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
    	
    	// Register the window class
    	if (!RegisterClassEx(&WindowClass);)
    	{
    		DebugTest[19] = (double)GetLastError();
    		return 1;
    	}
    	
    	WindowHandle = CreateWindowW("MyClass",
    		"Platform Masters 1.0 (development version)",
    		WS_OVERLAPPEDWINDOW |
    		WS_VISIBLE |
    		WS_SYSMENU,
    		WindowPos.x, WindowPos.y,
    		WindowSize.x, WindowSize.y,
    		NULL,
    		NULL,
    		hInstance,
    		NULL);
    	
    	DebugTest[19] = (double)GetLastError(); // returns 1407 - cannot find class
    	
    	if (WindowHandle == 0)
    	{
    		return 0;
    	}
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    The code you posted has errors.

    Code:
    if (!RegisterClassEx(&WindowClass);)  <<------  ; shouldn't be there
    WindowPos and WindowSize are not shown to be defined. When I placed your code in a generated hello world app, I used CW_USEDEFAULT for .x's and 0 for .y's. I also had to change the WinMain so it didn't use the InitInstance and RegisterClass functions that were generated. I also added a DWORD ret for your DebugTest[19], since it also wasn't shown to be defined. The program ran as expected. No errors, therefore the code you posted isn't the problem. (Assuming your positioning variables have valid values and the ; error above was just a typo here, not in your actual code.
    Last edited by DaveH; 05-08-2009 at 08:54 AM.

  3. #3
    tjb
    tjb is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    You're calling CreateWindowW, not CreateWindowA, so your class name parameter should be L"MyClass" (note the L prefix), not "MyClass". Also, although it may not be an issue depending on exactly how you're compiling the file, your "MyClass" in the context of WindowClass.lpszClassName = "MyClass" really should be _T("MyClass") (which expands to "MyClass" or L"MyClass", depending on the UNICODE preprocessor definition).

    Read up on CreateWindow. Any literal parameter that is LPTSTR or LPCTSTR should be written as "XXXX" if calling CreateWindowA, L"XXXX" if calling CreateWindowW, or _T("XXXX") if calling CreateWindow (which is just defined to be CreateWindowA or CreateWindowW, depending on UNICODE and/or _UNICODE).
    Last edited by tjb; 05-10-2009 at 01:09 AM.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Read up on CreateWindow. Any literal parameter that is LPTSTR or LPCTSTR should be written as "XXXX" if calling CreateWindowA, L"XXXX" if calling CreateWindowW, or _T("XXXX") if calling CreateWindow (which is just defined to be CreateWindowA or CreateWindowW, depending on UNICODE and/or _UNICODE).
    You're on the right track there. But:
    LPTSTR, LPCTSTR - _T("XXXX") - Found in structures, mostly.
    LPSTR, LPCSTR - "XXXX" - Found in the A versions of the functions, ie, CreateWindowA
    LPWSTR, LPCWSTR - L"XXXX" - Found in the W versions of the functions, ie, CreateWindowW
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21