Friday, February 22, 2008

Introduction

Win 32 API Programming:


Interface:

Any program is defined to be a set of instructions given by the programmer to the computer for processing.
The user who is using the program should see the result of the program or should interact with the program by giving some input to it.
Interface is where the user can interact with the program.

Any user can interact or use the program in two ways.
• With the help of Characters.
• With the help of Graphics.

The interface that contains characters is Character User Interface or CUI.

Example:

DOS operating system has character user interface.

The interface that contains graphics is Graphical User Interface.

Example:

Windows operating system has Graphical User Interface, this is because of the programs with a number of built in functions and data which is not present in other Character User Interface operating system.

Differences between DOS and Windows:

Interaction with hardware devices:

DOS programs directly interact with hardware like memory and printer ports.

Note:
Port is the location where we can interact with the hardware devices.




Windows provides device drivers and functions, so that our program can call them
Whenever they wanted to use.

Note:
Device drivers are special programs required to access any Hardware devices.
They will drive to and from the devices.

And the functions, provided by windows in order to use any hardware device are
called GDI (Graphical Device Interface) functions.

GDI functions internally take the help of a data structure called Device Context, in order to get the information about the hardware devices.

Windows maintains information about output devices in an internal structure called Device Context.

Programming model:

DOS Program model:

DOS programs call the operating system to get the input directly.
Whenever a DOS application starts complete control over the system resources like CPU, memory is given to it.

The DOS application waits for the user to input, then it takes a particular action.
Internally as it waits, it continues controlling the system resources and the control is not given to any other applications till the current application is closed.

Whenever an I/O (input output) event occurs, the information goes directly to the application but not to the operating system.


Windows Program model:

Where as windows programs process the input with the help of messages sent by the operating system.

Windows is responsible for all I/O events like mouse clicks, keystrokes etc…

When an event is generated windows figures out which window application is currently active and sends the information about the event to the current or active application.

The below figure shows that:

In other words, all messages that event is generated is stored in the form of a queue called system queue, which is maintained by windows.

From the system queue, the messages are then sent to applications by windows and stored in appropriate application message queues.

It is the responsibility of the applications to read the queue and process the messages.

Structure of window program

Structure of window program:

A Window program performs two basic tasks:

• Creation of the program’s window and doing the tasks like setting aside memory space when the program is first loaded.

• Processing messages, which are received, from windows?



Application Creation Stage at API Programming:

We first design the user interface objects like menus, tool bars, status bars etc. This in turn creates resource files.

Then we have to concentrate on the writing the functional code to handle messages, this code is nothing but the source code.

After creating, both of them i.e., resource code and source code, they are builds and then executable file is created.

The general outline of the Window program is as follows:

Create the program’s window
Do any initialisation of variables
LOOP
Fetch messages sent by windows to this program

Is the message QUIT?
If the message = QUIT
{
Terminate the program;
Result control to windows;
}
If the message is another message
{
Do the appropriate actions based on the messages;
Return control to windows;
}
ENDLOOP

Thursday, February 21, 2008

HANDLES In WIN32

Every running program is an instance or copy of the program.

This is also called object of the program. Windows should be capable enough to keep track of number of objects of a particular application in the memory.

So windows keep track of the addresses of different objects in the memory by storing their addresses, which is in the form of an unsigned integer.

This unsigned number, which is used to represent address of an object in the memory is called a HANDLE.

Handle is unique from one object to another. In windows handles refer to all objects.

Handle is pointer to a pointer to a memory location.

There are a number of data types in windows for describing the handles. These data types begin with a H.

Example:
HWND –handle to a window

Internally windows maintain tables that allow window to convert handles to physical addresses.



Example of Win32 API Program to display an message:

#include

int PASCAL WinMain(HANDLE hInstance , HANDLE hPrevInstance,LPSTR lpszCmdLine,int nShowCmd)
{
MessageBox (0,”Application is executing”,”Application”, MB_OK);
return 0;
}

Explanation of the Program:

Windows.h:-

Windows.h is the one of the important header file for window programs.

It contains definitions of hundred’s of window functions, definition of structures, constants, data types and derived data types.

This can be included using # include statement.

WinMain Function:

The function declaration is as follows

int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow);

WinMain is similar to main of C and C++.

Every window program must have a WinMain function. When a window program starts, it is the WinMain, which is executed first.

Whenever an application or a window program is executed the windows operating system executes Win Main method first and it (i.e.. operating system) will pass the necessary parameters to it, which can be used in the program later for processing the messages.


Parameters of WinMain: -


hInstance and hPrevInstance are two variables which are of HANDLE data type,that is there are capable enough to store addresses in the memory.

WinMain function will take these variables to store addresses of the different objects of that application.

hInstance is used to store the address of the current object or instance of the application.

In other words we can say that this is a handle to the current instance.

hPrevInstance is used to store address of the previous instance of the application.

In other words we can say that this is a handle to previous instance.

The third parameter lpszCmdLine is a long pointer to the string which in turn consists of command line parameters which are passed to the program if any,

lpsz stands for long pointer to string.


The fourth parameter nCmdShow is an integer value that is passed to another function called ShowWindow function.

Windows to convey the message that whether the program should appear minimised, maximised or normal uses this parameter when it is displayed.



Return value of WinMain:
The return value of the WinMain in int, it returns 0 if the function is successfully executed else 1.

PASCAL Keyword and MessageBox Function

PASCAL Keyword:

PASCAL is a compiler keyword.

It should be in uppercase.

It informs C compiler to use function-calling conventions of Pascal language but not C language.

The reason behind this is Pascal language’s function calling convention produces less code when compiled into machine instructions.

This is because the Pascal function calling conventions puts parameters passed by a function n the stack, in the order there are declared in function, in C calling convention this is the reverse.

As a stack uses LIFO (Last In First Out) principle.

In the case of Pascal, compiler always knows the first argument is at the end of the stack, while in C, the compiler has to search for the first parameter by checking each and every parameter in the stack ,so the extra number of CPU instructions are generated when the function is called.




MessageBox Function:

This function is used to display message on the message box.

The declaration of this function is the following :

int MessageBox(HWND hWnd , LPSTR lptext , LPSTR lpcaption ,WORD wType)

Parameters passed to MessageBox function:

The first parameter hWnd is the handle of the parent window.

0 can be used as first parameter if the message box belongs to the current window.

The second parameter lpText is the text, which should be displayed as a message.

The third parameter lpCaption is the caption of the message box window.

The fourth parameter wType is the type of the message box required.

The valid values, which can be used as fourth parameter, are

MB_OK --- to display a simple MessageBox
MB_ICONQUESTION -----to display Message box with a question mark as an icon.
MB_ICONEXCLAMATION ---to display MessageBox with a exclamation mark as an icon.
MB_ICONHAND----it display message box with cross mark as an icon.

Explanation of parameters

Explanation of parameters:

LPSTR lpclassname = It is used to specify the class name.

LPSTR lpwindowname = It is used to specify the caption of the window.

DWORD dwStyle = This parameter is used to specify the style how the window should be displayed. We generally the window style that is WS_OVERLAPPEDWINDOW.

int X and int Y = they specify the x, y co-ordinates of the starting point of the window.

int width and int height = They specify the width and height of the window.

HWND parent = It is used to specify the handle to the parent if we are going to create a child window.
If we are going to create a window for the first time or if it is parent window, this value would be NULL.

HMENU menuname = It is used to specify the handle of the menu, if it NULL it will take the menu name from WNDCLASS structure.

HINSTANCE hInstance = It is used to specify the handle to he application to which the windows belongs to.

PARAM param = It is used to parameters passed to the parent window from the child window.

SHOW WINDOW FUNCTION:

This function is used to show the window created by CreateWindow function, which takes two parameters.

The following is the declaration of the ShowWindow function

BOOL ShowWindow (HWND hWnd, int nCmdShow)

This function will return true that the window is displayed successfully else it will return false.

PARAMETERS :

HWND hWnd = It is the parameter which will specify the handle to the window that is to be displayed.

int nCmdStyle
= It is the parameter that specifies the window style how the window should be displayed.

It parameter is one of the parameter of the WinMain function.

It is an integer. By default has 1 as its value.

The parameter nCmdShow can have the any of the below values:

• SW_HIDE
• SW_MINIMIZE
• SW_RESTORE
• SW_SHOW
• SW_SHOWMAXIMISED
• SW_SHOWMINIZED


Now as we have completed all the functions required to create and show a window now the next task is to handle the messages send by the windows operating system to our application,

As discussed earlier the information that event is raised is sent from windows operating system in terms of messages to the active application.

In the application it is stored as a queue and uses a structure called MSG structure