window handle invalid

This is a discussion on window handle invalid within the Windows Programming forums, part of the Platform Specific Boards category; Hey all, just joined you. Anyhow, I'm trying to run a simple windows program, and simply cannot figure out what ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    7

    window handle invalid

    Hey all, just joined you.

    Anyhow, I'm trying to run a simple windows program, and simply cannot figure out what I'm doing wrong. I remember doing this stuff about 3 years ago, but I don't remember any of it :/

    Here's the code (there's more, but even this much doesn't work; I actually stole this from a tutorial online and it STILL doesn't work).

    Code:
    LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) 
    {   
    	switch(msg)
    	{
    		case WM_CLOSE:
    			DestroyWindow(hwnd);
    		break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    		break;
    		default:
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	hwnd = CreateWindowEx(NULL, "ProgName", "First Program",WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                            NULL, NULL, hInstance, 0);
    
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", NULL,
    			MB_ICONEXCLAMATION | MB_OK);
    		DWORD blah = GetLastError();
    		return 0;
    	}
    	return 0;
    }
    It compiles and links just fine, but when the program executes the message box pops up and the application closes right after I click ok. When i run through it debugging, I look at the value of hwnd and find it to be 0x0.

    Any help would be great! Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > DWORD blah = GetLastError();
    And the value of this would be what?

    And then you look it up on MSDN
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Whats your code for registering the window class?

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    Thanks salem, but I already tried that. the error code would be 0, which is not very helpful at all.

    @DaveH
    I was under the impression that if you did not specify a class, it would create a default. Either way, whether that's true or not, this code still did not work for me:

    Code:
    WNDCLASSEX wc;
    
    wc.cbSize = sizeof(WNDCLASSEX);
    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.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = "ProgName";
    wc.lpszMenuName = NULL;
    wc.style = 0;

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Probably because you got the last error from your message box, not the API call which actually failed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    ah, good call salem, thanks. I just remembered to put this line in my code:

    Code:
    RegisterClassEx(&wc);
    which was really stupid of me, sorry...

    anyhow, it doesn't have a null window handle any more and the message box no longer pops up, but the window still doesn't show up. it just runs and quits without showing anything.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by handle View Post
    ah, good call salem, thanks. I just remembered to put this line in my code:

    Code:
    RegisterClassEx(&wc);
    which was really stupid of me, sorry...

    anyhow, it doesn't have a null window handle any more and the message box no longer pops up, but the window still doesn't show up. it just runs and quits without showing anything.
    RegisterClassEx was what I was looking for. Did you also

    ShowWindow(...);
    UpdateWidnow(...);

    after creating the window?

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    Yes I did, but to no avail:

    Code:
    ShowWindow(hwnd,nCmdShow);
    UpdateWindow(hwnd);

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    Well I decided to walk through the callback procedure...it went through the method 4 times.

    1st time:
    msg = 36 (WM_GETMINMAXINFO)

    2nd time:
    msg = 129 (WM_NCCREATE)

    3rd time:
    msg = 131 (WM_NCCALCSIZE)

    4th time:
    msg = 1 (WM_CREATE)

    Then it doesn't go through the callback again. I step to the GetLastError, and it gives me error 87: "ERROR_INVALID_PARAMETER". Helpful. Where am I going wrong?

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    It is going to be very hard to tell what is going wrong unless we can see your all of the code.

    You say you did ShowWindow, UpdateWindow but its not in your code you've show, so I have no idea where you do it.

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    Okay, to clarify everything here is the full code. Sorry I've been a little frazzled trying to get this fixed quickly

    Code:
    // win main
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc;
    
    	wc.cbSize = sizeof(WNDCLASSEX);
    	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.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = "ProgName";
    	wc.lpszMenuName = NULL;
    	wc.style = 0;
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Window Registration Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	HWND hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "ProgName", "First Program",WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                            NULL, NULL, hInstance, 0);
    
    	ShowWindow(hwnd,nCmdShow);
    	UpdateWindow(hwnd);
    	DWORD blah = GetLastError();
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", NULL,
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    	return 0;
    }
    
    // and callback
    LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) 
    {   
    	switch(msg)
    	{
    		case WM_CLOSE:
    			DestroyWindow(hwnd);
    		break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    		break;
    		default:
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    	return 0;
    }
    And that should be everything I need to create a valid window correct? If not I am obviously missing something really simple.

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    You don't have a message loop. Just find the first random Win32 API tutorial in the internet and copy it from there.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  13. #13
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    Wow. I figured I was missing something stupid. Well, either way, now that I put that in there it doesn't quit automajically, but still no window shows up.

    Thanks tho

    (used this

    Code:
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    The following code works for me. It displays a blank window. (this is your code, adding in the things you said you added, and adding variables etc. so it would compile.) I also added a definition for WndProc and moved your checking of the created window handle before the ShowWindow uses the handle.

    Code:
    #include "stdafx.h"
    #include "resource.h"
    
    LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
    
    // win main
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	MSG Msg;
    
    	WNDCLASSEX wc;
    
    	wc.cbSize = sizeof(WNDCLASSEX);
    	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.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = "ProgName";
    	wc.lpszMenuName = NULL;
    	wc.style = 0;
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Window Registration Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	HWND hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "ProgName", "First Program",WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                            NULL, NULL, hInstance, 0);
    
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", NULL,
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	ShowWindow(hwnd,nCmdShow);
    	UpdateWindow(hwnd);
    	DWORD blah = GetLastError();
    	
    
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
    
    
    	return 0;
    }
    
    // and callback
    LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) 
    {   
    	switch(msg)
    	{
    		case WM_CLOSE:
    			DestroyWindow(hwnd);
    		break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    		break;
    		default:
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Child window handle not known
    By mvanrijnbach in forum Windows Programming
    Replies: 3
    Last Post: 03-10-2010, 05:31 AM
  2. Help with setting text into LTEXT using Window Handle
    By jasperleeabc in forum Windows Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. creating a child window
    By rakan in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2007, 02:22 PM
  4. Buttons + Edit Control
    By jay kay in forum Windows Programming
    Replies: 6
    Last Post: 03-09-2005, 04:36 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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