Thread: Program not showing up

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Program not showing up

    When I compile this program, a window doesn't open, instead, a message box that says, "Click box.exe has stopped working", I don't know why this is happening.

    Here is the WinMain function

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
    {
        WNDCLASSEX WinClass;
        MSG msg;
        LPSTR szClassName = "BoxApp";
    
        //Register the window class
        WinClass.cbSize = sizeof(WNDCLASSEX);
        WinClass.style = 0;
        WinClass.lpfnWndProc = WndProc;
        WinClass.cbClsExtra = 0;
        WinClass.cbWndExtra = 0;
        WinClass.hInstance = g_hInstance;
        WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        WinClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        WinClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        WinClass.lpszClassName = szClassName;
        WinClass.lpszMenuName = NULL;
    
        g_hWindow = CreateWindowEx(WS_EX_CLIENTEDGE, szClassName, "The Box App",
                                 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
                                 NULL, NULL, g_hInstance, NULL);
    
        ShowWindow(g_hWindow, iCmdShow);
        UpdateWindow(g_hWindow);
    
        while(1 == 1)
        {
            g_pBox->UpdateBox();
            InvalidateRect(g_hWindow, NULL, FALSE);
            if(GetMessage(&msg, NULL, 0, 0) > 0)
            {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
            }
        }
    
        GameEnd();
    }
    Thanks in advanced

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Add WS_VISIBLE to your window styles or use ShowWindow() with SW_SHOWNORMAL to make it visible.

    The show parameter of the command line only works if you have it set in a shortcut, otherwise it's 0.

    It's there, you just can't see it.

    Code:
    if (icmdshow > 0)
      ShowWindow(g_hwind,icmdshow);
    else
      ShowWindow(h_hwind,SW_SHOWNORMAL);
    Last edited by CommonTater; 10-07-2010 at 05:08 PM.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Nope, the problem still occurs

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok I'm assuming you have a window message procedure defined somewhere else in the code?

    I don't see a call to RegisterClass() which is essential before anything is going to happen. You can't even successfully create a window without it.

    Plus I rather suspect you're going to run into problems with your dispatcher loop... Invalidating the window at that point is going to make it constantly redraw itself, causing quite the light show... plus there's no exit provision for when WM_CLOSE or WM_QUIT are processed, leaving you with no exit from the program.

    At minimum you need a window class, one window, a message dispatcher and a message tosser before a windows program is going to run. The example below comes from theForger's Win32 API Tutorial Less than this and you get nothing.

    Code:
    #include <windows.h>
    
    const char g_szClassName[] = "myWindowClass";
    
    // Step 4: the Window Procedure
    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)
    {
    	WNDCLASSEX wc;
    	HWND hwnd;
    	MSG Msg;
    
    	//Step 1: Registering the Window Class
    	wc.cbSize		 = sizeof(WNDCLASSEX);
    	wc.style		 = 0;
    	wc.lpfnWndProc	 = WndProc;
    	wc.cbClsExtra	 = 0;
    	wc.cbWndExtra	 = 0;
    	wc.hInstance	 = hInstance;
    	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    	wc.lpszMenuName  = NULL;
    	wc.lpszClassName = g_szClassName;
    	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Window Registration Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	// Step 2: Creating the Window
    	hwnd = CreateWindowEx(
    		WS_EX_CLIENTEDGE,
    		g_szClassName,
    		"The title of my window",
    		WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
    		NULL, NULL, hInstance, NULL);
    
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	ShowWindow(hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    
    	// Step 3: The Message Loop
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    	return Msg.wParam;
    }

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Ok thanks, that was what was causing the error

    Edit: Ops, I'm sorry for this, the window show up, but it still says "Click box.exe has stopped working". I didn't put that in the response earlier because it was a few seconds before the error showed up, and by then I needed to catch the bus.
    Last edited by bijan311; 10-08-2010 at 03:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to execute other program within c program?
    By KoYoungSuk in forum C Programming
    Replies: 7
    Last Post: 06-07-2010, 05:08 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  5. Replies: 3
    Last Post: 01-14-2003, 10:34 PM