Topic : A VERY Basic Windows Program
Author : Barry Jacobsen
Page : 1

       
Hello.  How are you?  Good?  That's good.  Have a seat, won't you?  There now, that's better.  Today we are going to make a little windows program that will have absolutely no real world value.  Why, you ask?  Because you just might learn something if you've never made a windows program before.  First, I'll swamp you with the code, then I'll explain line by line what it does.  Sound good?  NO?  Well, too bad, that's how it's gonna be.  By the way, you should know C before even attempting this.  All this code works in the latest version of Visual C++, I make no guarantees it works elsewhere.

Here we go:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
int result = ::MessageBox(NULL, "This program displays your system directory, continue?", "Continue?", MB_OKCANCEL);

if (result == IDOK)
{
const int BUFFER_SIZE = 255;
char bufferforapi[BUFFER_SIZE];
char bufferforsprintf[BUFFER_SIZE];

::GetSystemDirectory(bufferforapi, BUFFER_SIZE);

sprintf(bufferforsprintf, "Your system directory is: %s.", bufferforapi);

::MessageBox(NULL, bufferforsprintf, "Useless information you should already know.", MB_OK);
}
else
{
::MessageBox(NULL, "Operation Aborted.", "Abort, Abort!", MB_OK);
}

return 0;
}


Now what the heck does that do, you ask?  Well, lets find out.

#define WIN32_LEAN_AND_MEAN
This line will exclude a bunch of code bloat that Microsoft thinks you want.

#include <windows.h>
This line includes the standard stuff needed to use windows

#include <stdio.h>
Standard IO(Input/Output) routines, namely sprintf in this program.

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
This line is a line that must be included in EVERY windows program, it is the starting point in your program.

int result = ::MessageBox(NULL, "This program displays your system directory, continue?", "Continue?", MB_OKCANCEL);
This line asks you if you want to continue and stores the result in a newly created varible "result".  How do I know what parameters to pass?  Well, lets look at what MSDN has to say:
int MessageBox(
  HWND hWnd,          // handle to owner window
  LPCTSTR lpText,     // text in message box
  LPCTSTR lpCaption,  // message box title
  UINT uType          // message box style
);

Well, this program doesn't have a window, so we just give it NULL for the owner Window, the message, the Title, and the Style.

if (result == IDOK) {
Make sure the user clicked Ok.

const int BUFFER_SIZE = 255;
This is a constant makes it easier to change the size of the text buffers.

char bufferforapi[BUFFER_SIZE];
This is a text buffer to store the result of the call to ::GetSystemDirectory

char bufferforsprintf[BUFFER_SIZE];
This is a text buffer to store the result of the call to sprintf

::GetSystemDirectory(bufferforapi, BUFFER_SIZE);
This is another one of those nasty API calls, let's see what MSDN has to say:
UINT GetSystemDirectory(
  LPTSTR lpBuffer,  // buffer for system directory
  UINT uSize        // size of directory buffer
);

Okay, simple enough, we are just passing a place for the text to go, and how much text that place can store.

sprintf(bufferforsprintf, "Your system directory is: %s.", bufferforapi);
This copies the system directory into the other buffer and adds some text so it looks pretty.

::MessageBox(NULL, bufferforsprintf, "Useless information you should already know.", MB_OK);
Displays the message we have worked so hard to make.

} else { ::MessageBox(NULL, "Operation Aborted.", "Abort, Abort!", MB_OK); }
If the user didn't want to continue, just abort.

return 0; }
This must end every windows program, it exits the application.

Well, there you have it.  I hope you learned something.  If not, you can call me a moron or ask for further assitance at BarryJ@ureach.com

By the way this document is Copyright 2001 to Barry Jacobsen.  BOOL StealingIsGood = FALSE;


Page : 1