CGI using MFC


We are real fans of writing programs using the MFC classes, the reasons being :-

The only problem with the MFC classes is, they are too high level for them to be your first experience of CGI programming. We are going to be writing CGI code here but I suggest you don't read this stuff unless you know a bit about CGI and the MFC classes and more importantly, unless you have read our isapi tutorial. Once you know the basics well, you can go on to do this stuff. It is classes like these that make us believe that Microsoft has won the battle for the Internet.

Lets make a very political statement here. I don't like UNIX. I don't like it for the simple reason that I've been spoilt by the user-friendliness of Windows 95, and Windows NT. It's only people with seven or more years experience on UNIX that don't have a problem with it. I've got more than seven years experience, but I moved on to something better.

Now lets look at a small and very short CGI program. All that you have to do is you click on a button in your browser. That button calls submit internally, which will then call a program, or it will ask for an executable file that will run on the server . The method could be either get or post. The only difference between a get and a post is that if it is a get, whatever the user typed in comes after the question mark part of the URL and if it is a post , it comes as a part of data.

The reason people used perl to write CGI programs was because there was a lot of string processing to be done to get and process the data. e.g. If you have 3 input boxes called a b c then it would be a="good"&b="bad"&c="ok" where good,bad and ok are values to a , b , c and it will be separated by &'s. The first job in perl will be to figure out what the user wrote, and then create an HTML file which the program will give to the server and which the server would then pass it on to the browser. Now all that is great but doing it in perl is very messy. ( you could do it in perl because perl was meant for string processing. )

What we are saying is that you don't need to write CGI program in perl because perl has a syntax which takes a long time to get used to. And with due apologies to the guys who wrote it, I don't like the way it looks. That is a very subjective point, but true none the less. But we have done a tutorial on it and anyone who wants to do perl please go ahead. Now the problem with perl and any other scripting language is that an executable program has to be run on the server . Now if it was a .dll , life would have been so much easier and it would be so much faster because .dlls once loaded into memory can be reused again and again.

Creating the html file

a.htm

<form action=abc.dll?aaa   method=POST>
<input type=text name=aa><p>
<input type=text name=bb><p>
<input type=submit value="Click..">
</form>
What we now recommend you do is create a .dll and in that .dll have two functions which are to be exported. One will be GetExtensionVersion and the other, HttpExtensionProc. So the .def file will be

abc.def

LIBRARY      "abc"

EXPORTS
	HttpExtensionProc
	GetExtensionVersion
abc.cpp
#include <afxwin.h>
#include <afxisapi.h>
#include <afxole.h>
#include <iostream.h>
#include <assert.h>
class zzz: public CHttpServer
{
public:
zzz() {}
BOOL GetExtensionVersion(HSE_VERSION_INFO* pVer)
{
strcpy(pVer->lpszExtensionDesc, "hi");
return TRUE;
}
void aaa(CHttpServerContext* pCtxt,LPCSTR b,LPCSTR c)
{
*pCtxt << b;*pCtxt << c;
}
DECLARE_PARSE_MAP()
};
BEGIN_PARSE_MAP(zzz, CHttpServer)
ON_PARSE_COMMAND(aaa, zzz,ITS_PSTR ITS_PSTR)
ON_PARSE_COMMAND_PARAMS("aa=~ bb=~")
DEFAULT_PARSE_COMMAND(aaa, zzz)
END_PARSE_MAP(zzz)
zzz a;
In the .cpp file, we have first the header files. Next we create a class zzz that looks like HttpServer ( you require its prototype and the code which comes bundled with Visual C++ 4.2 ). We are happy that Microsoft lets you have a subscription for VC++ 4.2 ( if you have an official copy of Visual C++ 4.0 ) in India also. This means we finally get subscriptions at the same time as people in other countries. Now in zzz, we have first a function called GetExtensionVersion where we simply do a stringcopy. (This is not very important, all that you do is initialize a pointer to a char. We wrote 'Hi', you write what you like ).

Now the most crucial things here are the macros DECLARE_PARSE_MAP, BEGIN_PARSE_MAP and END_PARSE_MAP. This is exactly equivalent to what the message maps were, the only difference is that they now do something different.

The most important line within BEGIN_PARSE_MAP and END_PARSE_MAP is the first one i.e. ON_PARSE_COMMAND. This is again a macro. If you look into one of the header files, it will give you some dirty code, exactly like the message maps do. We give this macro the name of the function (we have said aaa ) and then the name of the class ( we have said zzz because that is the class in which the function aaa is ). Now we want two parameters to be passed to this function aaa, so we use ITS_PSTR which is a #define, it says that the parameter the said function will receive will be a string. (It is nothing but a #define).

In the next line, is another important macro ON_PARSE_COMMAND_PARAMS. Now here we give just a simple string, i.e. aa=~ and bb=~ . Now that is syntax, where aa and bb are the names that we have given to our input boxes in the HTML file.

That's all that there is in this program, Now if you see the HTML file ( a.html ) you will find the two input boxes, plus the submit button. The action says abc.dll?aaa. Now you don't have to know what is happening internally when you click on the submit button. All you need to know is that function aaa will be called automatically. The function aaa is first given a pointer to CHttpServerContext, which is a handle ( i.e. we don't have to know what it means ). Then we have two variables which are pointers to chars whose values we display as the answers. Now the point we are trying to make is that you don't have to parse your input string, you know what aa and bb are. You don't have to do anything beyond that and you can do what you want to do, like sending out whatever HTML file you want etc.

Now you can add one more line to you BEGIN_PARSE_MAP, END_PARSE_MAP, that says DEFAULT_PARSE_COMMAND. We have said aaa (function) , zzz (class) which means that if you don't specify a question mark and the function name after the dll, then by default it will call this function called aaa. Simple, isn't it ?

In VC++ 4.2, there are a large number of classes we haven't yet explored. Depending on what new software Microsoft releases, we'll keep providing information on the others.


The tutorial is a joint effort of
Mr. Vijay Mukhi
Ms. Sonal Kotecha
Mr. Arsalan Zaidi


Have any suggestions, comments, ideas, cracked code, feedback. Feel free to get in touch with us.

Move back to the Vijay Mukhi's Technology Cornucopia Page to learn more about the other new Internet Technologies.


Vijay Mukhi's Computer Institute
VMCI, B-13, Everest Building, Tardeo, Bombay 400 034, India
E-mail:vmukhi@giasbm01.vsnl.net.in Tel : 91-22-496 4335 /6/7/8/9 Fax : 91-22-307 28 59
http://www.vijaymukhi.com