-
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
-
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?
-
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
-
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.
-
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
-
Let's see some source code.
-
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
-
You need to use the equality operator(==) rather than the assignment operator(=).
Code:
if (flag == 0)
{
return (0);
}
-
Ha! Thanks! Duh! :-)
It works fine now! :-)
mw