Author : Vijay Mukhi
Page : << Previous 2 Next >>
in ' C ' which says that when you push parameters on the stack, you push it in the reverse order. These rules which define how the parameters are to be pushed on stack, are known as ' calling conventions '. So now, we also have to specify which calling convention, our function should use. In our program unfortunately, the WinMain follows the Standard Calling Convention, which is different from the ' C ' Calling Convention. Since our program is a ' C ' program, all functions are assumed to be called in the ' C ' Calling Convention. But if we are to call a function in the Standard Calling Convention we have to put ' _stdcall ' before it.
So here in our program we call the function in the Standard Calling Convention method.
#include<windows.h>
_stdcall WinMain(HINSTANCE i,HINSTANCE j,char * k, int l)
{
}
As soon as you put in ' _stdcall ' in front of the function you'll see that the colour of the word ' _stdcall ' changes, this shows that it is a reserved word in Windows ' C ' program and it has a special meaning to it .' C ' understands what ' _stdcall ' is. Now when you compile the program you will not get any errors. You can now go ahead and build the program. To your relief there is still no errors.
Now execute this program by choosing ‘ Build ‘ from the menubar and select ‘ Execute .exe ‘. On execution, you will see that the program does not do anything. This is because there are no lines of code within ‘ WinMain() ’.
Now to make our program do something useful, we add a MessageBox into our program which appears as shown below.
#include<windows.h>
_stdcall WinMain(HINSTANCE i,HINSTANCE j,char * k, int l)
{
MessageBox(0,"Hi","Bye",0);
}
Let us understand what is the code we have put inside our ' WinMain() '. We have put in a MessageBox which is a function that is understood by ' C '. This function has to be passed four parameters as part of it's syntax. Here we give you a bit of advise that we learnt very early in life. " Never argue with syntax ". The first and last parameters, which are both ' 0 ', need not be understood by us at this stage. So, forgetting these parameters - though we have to pass them in our function as part of the precious syntax - we can go to the second and third parameters and insert two strings ( you can pass any values you wish, but they should be such that they could be passed as strings). If you want to know why we used "Hi" and "Bye" as the parameters we can also provide you with an explanation. We have passed these parameters as they were the first thing to come to our mind and they looked very friendly. You can pass any string you want, we are not stopping you in any way. Now continuing with our program, why don't you build and execute it ?
Ah! What do you see ? What you now see, is a cute little MessageBox with "Hi" and "Bye" in it .The MessageBox disappears as soon as you click on the O.K. button in it. So finally, we have managed to get our program to do something for us.
If you are in a mood of doing some R & D, we suggest that you try changing the last parameter from 0 to 1 or 2 or 3 and so on till you get exhausted.We have tried our best to reach the last limit of this parameter,but we got exasperated after we reached 1000000, and decided not to go any further. Do us a favour by not asking us as to how far you can go with these numbers. Do not, however, change the first parameter at present. When you change the last parameter to 1, you do not get any errors after the compilation and linking process. On executing the program, you see that there is a change in the number of buttons displayed in the MessageBox. Similarly, as you keep changing this last parameter, you will see that the appearence of the MessageBox keeps changing with only the messages remaining unchanged.
Now let us change the code in the function as under;
#include<windows.h>
_stdcall WinMain(HINSTANCE i,HINSTANCE j,char * k, int l)
{
char aa[100];
sprintf (aa,"%d", i);
MessageBox(0,aa,aa,0);
}
In this program we have defined an array ' aa ' of 100 characters. Let us see how we can store some value into it. So in our next line of code, the function ' sprintf ' is used to store the value of ' i ' - which is the first parameter of our function ' WinMain() ' - in the array ' aa '. Now to give you an explanation of what the functions ' fprintf() ' and ' sprintf() ' do, would be an insult to your intelligence. Hence, they are not explained here (you already know what they do, as it is assumed that you know ' C ' under DOS). Now when you build this program and execute it, you see a number displayed in the MessageBox. This number, is a number assigned to ' i ' by Microsoft Windows, and of no importance to us, but to Windows it is an identifier. It tells Windows who we are, and the first parameter we have passed to ' WinMain ' is this identifier. This identifier had to be given a name. It is called a handle in programming parlance, and so we shall also call it a handle . When we say that ' i ' is a handle in Windows, you have to realise that you should not touch that variable. When we say ' not ', it means that you should not touch this variable, do not try to reassign a value to it or try to increment it’s value or do any other thing to this variable. It is to your advantage that you leave this handle alone.
Creating a window...
Let us now proceed to somthing big - like creating our own window. Whenever we write "Windows" with the "W" in capitals, we are referring to the Microsoft Windows System. But when we write "window", we refer to our window application. In ‘ C ’ under DOS we know that you faced many problems while creating the window. In Windows programming you will see that the creation is a bit easier. We already have two lines of code as our base. Let us go ahead with it and include some lines, one step at a time till we create our window.
#include<windows.h>
WNDCLASS a;
_stdcall WinMain(HINSTANCE i,HINSTANCE j,char * k, int l)
{
}
You have the right to wonder what ' WNDCLASS ' means. Please bear with us for the moment and build this program. What do you find? You get no errors. This proves that ' WNDCLASS ' is something which is understood by ' C ' Windows. Let’s also understand what it is that ' C ' understood. ' WNDCLASS ' is written in capitals and so we can say that it is a Macro. Where can a Macro be found? It is needless to say that it is found in the header file < windows.h >. Now since it is present in the header file, you may want to know what it could be? It could be a long, or an unsigned int or anything. ‘ WNDCLASS ‘ is none of the above, it is a structure tag and has it's own set of pre-defined variables ( If you want to know what a structure tag is, we advise you to get your fundamentals of ' C ' under DOS cleared ). Now by passing the above line we have created a structure ' a ' which looks like ' WNDCLASS '. We have chosen ' a ' as the name of our structure, because ' a ' is the first alphabet in English and we felt that it should be accorded some respect.
Now when you run this program you do not see any output. You may wonder, why even after creating a structure, ' C ' under Windows is stupid and does not give you an output? But then you have merely created a structure, you have not initialised any of it’s member variables and so you can't expect ' C ' to create a window merely because you created a structure.
Initialisation process...
Let’s move ahead and put a line of code in our function and try to initialise some of the members of our structure.
#include<windows.h>
WNDCLASS a;
_stdcall WinMain(HINSTANCE i,HINSTANCE j,char * k, int l)
{
a.hInstance = i ;
}
Now fold your hands, and pray that you don't get any errors. Finish your prayers and compile the program. Great! your prayers have been answered. You get no errors. Is this a miracle? Well let us try to explain this great phenomenon. You
Page : << Previous 2 Next >>