Topic : C Windows Programming
Author : Vijay Mukhi
Page : 1 Next >>
Go to page :

' C ' Windows Programming

' C ' Windows Programming ? Programming under Windows ? Do you really wish to learn ' C ' Programming under Windows ? Well, you have come to the right place. Now that you have finished learning ' C ' programming under DOS, it is high time you learnt something better. We will try to make it easier for you but we hope that the batch of " anti-frustration " tablets - which you most probably had used while learning ' C ' under DOS - is not exhausted . If it is, we advise you to get a fresh batch now, and we assure you that you will need it now, more than ever before. Learning ' C ' programming under DOS, is not easy, as you undoubtedly know. But then, you should know that if you want to learn something better, it is bound to be more difficult, and you would have to work harder to understand it. So here we are, trying to help you understand ' C ' programming under Windows.

These programs are written and explained with some assumptions, one of them being that you have thoroughly understood the concept of ' C ' programming under DOS and are well versed with it's functionality. With this assumption, we shall go ahead and try to understand what ' C ' programming under Windows is all about.

Creating your workspace...

Before starting with the programming part, you have to know where to type out the program. For this you have to follow the steps given below.

Click on Start and select Programs.
Next, select Microsoft Visual C++ 5.0
From here, select Microsoft Visual C++ 5.0 once again
Now click on File and select New.
You will now see a tabbed Dialog Box with Project already being selected.
From here, choose Win32 Application, type in the name of your project in the first edit box on the right hand side of this dialog box, and click on O.K.
Again, select File and select New.
You will now see the same tabbed Dialog Box but with File being selected.
Here, choose the Text File option, and put in the name of the file in the second edit box on the right. You have to end the file name with ' .c '
You have now entered the world of ' C ' Windows Programming. There is no turning back. You have to go through this ordeal if you wish to learn at least something about ' C ' programming under Windows.

Getting started...

Let us start with a small program in ' C ' under Windows

WinMain(HINSTANCE i,HINSTANCE j,char * k, int l)

In a ' C ' program under DOS, you know that the first function to be called is ' main()', and may start to wonder where is ' main() ' in our program. Before you confuse yourself - trying to make some sense out of our program - please continue reading.

In ' C ' Windows programming, the first function to be called is ' WinMain '. We already have this function in our program (It should not take too much time to dawn upon you, that it is a function. This is because we all know that a function begins and ends with an open brace '{ ' and a close braces ' }' ) . Now some genius amongst you will undoubtedly ask as to why the function should be called ' WinMain ' and not ' xyz ' or any other name ? This being a very intelligent question, we feel that the answer should also be equally intelligent. Many different answers come to our mind. But the best explanation we choose to give is as follows.

The person who had designed Windows programming , visited a very well known astrologer, who after searching for some signs and reading scriptures, advised him to give the function a name of seven characters in length and with two of the characters in capital letters. This person promptly thought of naming it ' WindowMain ' - Windows, because it is a Windows program and Main because it is the first function that is called in a ' C ' program . But then, he realised that the number of characters were more than seven, so he simply shortened it to WinMain.

Now if you believe this explanation, we can safely assume that you will belive anything we say, and we can also safely assume that you can be fooled easily ( this being a blessing in disguise, as there are some instances where we are forced to ask you to trust us, so that we can explain the concept better). Here, all we can say as to why it is called WinMain is, we do not know. The only person who can answer this supposedly ' intelligent ' question, is the person who designed Windows programming. But then, as we do not know who he is - though we tried our level best to find out who the person is - we cannot come up with the actual reason. If you know who the person is, you can get back to us with his name and address, so that we can ask him the reason why he named it

' WinMain ' and not xyz or whatever.

Getting back to the program, we are saying that we have created a function called WinMain and it is passed four parameters viz. HINSTANCE i,, HINSTANCE j, char * k and int l. Now, why we took i,j,k,and l as the variables is also an interesting question. This is mainly because we had decided to use these variables some years ago - due to some good reasons - and since then, we have faced no problem while using these variables. The reason, as to why we had decided on these particular variables, and not any other, has been forgotten. But, if by any chance, we happen to remember, and you are still interested, we'll let you know. Until then, we shall continue using the above variables, but you are free to use any variable you like.

Now, whoever calls the function ' WinMain() ', puts the parameters of the function on the stack, and they take up a certain amount of space on stack We shall explain what these parameters stand for. The first parameter HINSTANCE, is an unsigned int. If you search through the header file <windows.h> you will find this parameter as a #define HINSTANCE = unsigned int. This parameter is written in uppercase, we have not used the lower case, so you should also write it in the uppercase only. This is because of the fact that anything written in upper case in ' C ' is assumed to be a ' Macro ' and by convention all Macros are written in the uppercase.

Before venturing into any field, it is always better to invoke the blessings of God. So after offering prayers to God, and deciding on the auspicious time to start with your programming, type this program down. To compile a program under ‘ C ‘ Windows, click on the menu option ‘Build’. In the box which drops down, select ‘Build .exe ’. This will compile as well as link your program. After compiling this short program in ' C ' under Windows, what do you see? You see some errors on your screen. This is not an auspicious start.

But please do not panic and start praying all over again. The Gods are not displeased with you. There is a saying in English " To err is Human ". These errors are human errors. We have erred in the sense that we have not included the required header file - where the macros are defined - into our program.

  Inclusion of header files...

So now we include that header file in our program

WinMain(HINSTANCE i,HINSTANCE j,char * k, int l)
{ }

Now on compiling the program, you still get an error. But to your relief, you see that there is only one error. This goes to prove that the Gods have at least partially heard your prayers. Before you start wondering what went wrong , you have to understand what ' C ' Windows programming is all about. In ' C ' programming, there is something known as a ' calling convertion '. This calling convention is something like Diplomacy. Diplomacy means, that whenever you come into the presence of a Queen, you should go down on your knees.

There is also a protocol in Diplomacy which specifies that when a President of a country comes to India, then it is the President of our country and not the Prime Minister who receives him. The President of India does not have any power, but as he is the first citizen of our country, it is proper that it is he who should meet the President of that country. Similarly, there is a rule

Page : 1 Next >>