Termination

This is a discussion on Termination within the Windows Programming forums, part of the Platform Specific Boards category; At the beginning of my game I do some system checking and was wanting to terminate the program if something ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    207

    Termination

    At the beginning of my game I do some system checking and was wanting to terminate the program if something is wrong.

    This checking is done before a window is opened, etc.

    I was just wondering if there's a command to stop the program? PostQuitMessage will terminate the program but it waits until after the initialization is done and the window is opened.

    I wanted to terminate the program before the window is opened.

    My initialization program is seperate from my main program and is called from the main.

    mw
    Blucast Corporation

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Well, one way to stop the window from popping up would be to avoid calling ShowWindow();

    Example:

    Code:
    if ( CommenceSystemChecking() == ERROR )
      PostQuitMessage(0);
    else
      ShowWindow();
    Does that help?

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    207
    It's pretty strange what happens:

    My initialization program is seperate from my main program. Both programs have been added to the project.

    The first thing I do in the main program (in WinMain) is call the initialization function in my initialization program.

    I already have an IF statement in my initialization function that basically says:

    Code:
    if (!initialized) PostQuitMessage (0);
    (that's the basic idea; I typed that from memory)

    The main program quits if the initialization fails. HOWEVER, it creates the parent window before the PostQuitMessage takes effect. Since I called the initialization function before I even created the parent window, then it should terminate without my seeing the parent window.

    I don't know why it's decided to be a pain in the butt (other than the fact that Microsoft PROGRAMS Windows to be a pain in the butt).

    mw
    Blucast Corporation

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    As its name suggests, PostQuitMessage simple posts a WM_QUIT message to the thread's message queue. This will sit in the message queue until it is checked by GetMessage or PeekMessage. If you want to terminate immediately before a window is created, just return from WinMain or call the exit function from <stdlib.h>.

    Typically, it is far preferable to return from main rather than call exit from a sub-function.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ok, this probably sounds newbish but I seem to still have problems terminating my program:

    My initialization program has an int variable in it named "flag".

    If initialization is successful then flag == 1. Else, flag == 0.

    I return flag at the end of the initialization.

    When I get back to the main program, I IMMEDIATELY check the value of flag but it has no effect!?

    I'm pretty sure that I'm having an issue with scope, but I don't know how to resolve it... :-(

    mw
    Blucast Corporation

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Let's see some source code.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ok, here's the main program:
    Code:
    #include "Initialization.h"
    #include "resource.h"
    
    
    /*
    
    Project 0, Main
    
    */
    
    //prototypes
    LRESULT CALLBACK WndProc (HWND hwndMain, UINT message, WPARAM wParam, LPARAM lParam);
    
    //global variables
    static TCHAR szAppName[] = TEXT ("Project 0"); //name of window class
    RECT         rect; //
    HWND         hwndMain, hwndNew, hwndLoad, hwndOptions, hwndQuit, hwndOptionsBack; //handle to a window
    HDC          hdc, hdcBitmapMem; //
    MSG          msg; //structure for MSG
    WNDCLASS     wc; //structure for WNDCLASS
    int          cxScreen, cyScreen;
    int          btnHeight = 30;
    int          btnWidth = 110;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdline, int iCmdShow)
    {
    	Hard_Drive ();
    
    	if (flag = 0)
    	{
    		return (0);
    	}
    
    	//register a window class
    	etc...
    Right now the only thing I initialize is the Hard_Drive (hence the function call). I truncated the remainder of the program, since that works fine.

    Here's the initialization program:
    Code:
    #include "Initialization.h"
    
    int Hard_Drive (void)
    {
        bReturn = GetDiskFreeSpace
    		(
    		TEXT ("C:\\"),
            &dwSectorsPerCluster,
            &dwBytesPerSector,
            &dwFreeClusters,
            &dwTotalClusters
    		);
    
        if (bReturn)
        {
            /* cast it to 64 bits for calculations */ 
            i64TotalBytes = (__int64)dwTotalClusters * dwSectorsPerCluster * dwBytesPerSector;
            i64AvailableBytes = (__int64)dwFreeClusters * dwSectorsPerCluster * dwBytesPerSector;
    
            //printf ("Available = %I64u MB\n", i64AvailableBytes / (1024 * 1024));
            //printf ("Total  = %I64u MB\n", i64TotalBytes / (1024 * 1024));
    
    		flag = 1;
    
    		if ((i64AvailableBytes / (1024 * 1024)) < 16000)
    		{
    			MessageBox (NULL, TEXT ("ERROR! Not enough hard drive space!"), NULL, MB_ICONERROR);
    
    			flag = 0;
    		}
        }
    	else
    	{
    		flag = 0;
    	}
    
    	return flag;
    }
    That's the entire initialization so far... I have it set to give me an error if the available space is less than 16 gigabytes. I have 15 gigabytes remaining on my hard drive, and this forces an error to occur so that I can test this.

    Here's the header file that both the main and the initialization program include:
    Code:
    #define WIN32_LEAN_MEAN
    #define VC_LEAN
    
    #include <windows.h>
    #include <stdio.h>
    
    //Hard drive initialization declarations
    //======================
    unsigned __int64 i64TotalBytes, i64AvailableBytes;    
    BOOL  bReturn;
    DWORD dwSectorsPerCluster, dwBytesPerSector, dwFreeClusters, dwTotalClusters;
    int Hard_Drive (void);
    int flag;
    //======================
    
    #endif
    Last edited by Lionmane; 10-27-2005 at 08:14 PM.
    Blucast Corporation

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You need to use the equality operator(==) rather than the assignment operator(=).
    Code:
    	if (flag == 0)
    	{
    		return (0);
    	}

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ha! Thanks! Duh! :-)

    It works fine now! :-)

    mw
    Blucast Corporation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. zombie to exist after the termination of main program..
    By anilchowdhury in forum Linux Programming
    Replies: 0
    Last Post: 02-22-2008, 12:35 PM
  2. Replies: 4
    Last Post: 06-23-2006, 08:03 PM
  3. **///Re: Abnormal prg termination
    By jhsurti in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 02:33 AM
  4. abnormal program termination
    By ProLin in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2002, 09:56 PM
  5. Termination
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 10-14-2001, 09:24 AM

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