[Main]

Win32 Applications

There are several Win32 application programs types: Regular Windows Application, a Console Window, NT Device Driver, POSIX compliant application, Windows CE specific application,etc.  Most of our applications will either be a regular windows application or a console window application. 

A regular GUI windows application is a Win32 type application that usually presents a Window and interacts with the user using the Microsoft GUI.  You may if you wish have the program run start silently in the background performing various background tasks.  For instance, when you open a Microsoft Notepad is an example of a a regular GUI windows application.  If you have installed sound on your system, you will see a little speaker icon next to the time in the task bar.  This icon is actually a regular windows application called systray.exe that draws the icon in the task bar.  All regular windows applications will have the function WinMain as the user startup function.   To build a regular windows application in Visual Studio, create a Win32 Application project.  Your startup WinMain function must be written as in the box below. 

Normal Window Application Startup Code

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmd,int nCmd)

{

// Place Your program code Here...

return 0;

}

 

This type of windows application cannot use the stdio library functions that need access to stdin, stdout, and stderr.   For these functions(ie printf, scanf) to work properly, you will need to create a console window(See Several Programs Below).

A console application is a text based with a cursor and standard input, output, and error that all UNIX programmers are familiar with.  A console application has a single console window where all input, output, and errors are displayed in.  A console window looks much like a telnet session window.  All the standard handles expected in the stdio library for stdin, stdout, and stderr are correctly directed to the console window.  To build a console application in Visual Studio, you project must be an Win32 Console Application.  All console application projects execute the user startup routine called main.  The coding for a console application looks much like a regular C program:

Console Application Startup Code

#include <stdio.h>

int main(void)

{

// Place your code here.

return 0;

}

Even though both a regular Windows application and Console application seem to be totally different codes, they are not.  All applications have a default startup code that is set during the link-phase.  This startup code can be selected manually with the link command :  Link /entry:<Name of Startup Function>.    When you select to create a Win32 Windows application, the linker is told to call a startup code(found in a static library).  This startup code performs some runtime initializing and then calls the method WinMain.   WinMain is the user function where all user code starts.   When you select to create a console application in visual studio, the linked is told to link to another piece of code, found in a static library.  This code initializes the runtime environment for your application, creates a new console window, assigns the standard I/O handles, and finally calls the user function main.

So, technically there are no difference between console and window applications.   They all start off the same way.  If you need more control over a newly created application, you may want to set your own function entry point for your code.  

A console window is created with the AllocConsole function.  The FreeConsole function is used to close a console window.  Only one console window can be assigned to any one process, even if the process contains multiple threads.

Prototype:

BOOL AllocConsole(VOID);   // Creates a new Console Window, if one has not already been created

// Return value=TRUE on success

BOOL FreeConsole(VOID);    // Close a console window

// Return value=TRUE on success

Example Program

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpszCmd,int nCmd)

{

AllocConsole();   // Create a new console window

Sleep(5000);  // Sleep for 5000 ms=5 s

FreeConsole();  // Close the console window

return 0;

}

The following application demonstrates a program that calls a main function from a regular windows application.  Notice that the stdio library function freopen is used to open the the standard I/O handles.   The code works by using the pseudo file names "CONIN$" and "CONOUT$".  After a console window is created, these file names can be used to open stream input and output handles to a console window. 

Example of Window Creating a Console Window

#include <windows.h>

#include <stdio.h>

int main(void);   // Declare a main function

 

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpszCmd,int nCmd)

{

AllocConsole( );  // Create Console Window

 

freopen("CONIN$","rb",stdin);   // reopen stdin handle as console window input

freopen("CONOUT$","wb",stdout);  // reopen stout handle as console window output

freopen("CONOUT$","wb",stderr); // reopen stderr handle as console window output

 

main( );   // Call a regular C main function

 

FreeConsole( );  // Free Console Window

return 0;

}

 

As GUI applications never use a console window, we will always write console applications that generate GUI interface windows.  By writing all GUI windows application as console applications, we can easily include debugging statements(trace statements) in our code.  This is not strictly required because special macros, such as TRACE, can be used to send output to the Output Window in the Visual Studio environment.  But for learning purposes, console applications are the most useful. When necessary, I will return to writing regular GUI code with out console windows.

copyrightę2001 Prof Devi - mdevi@comine.com, mdevi@liu.edu. All rights reserved