MFC Extensions Hit Light Speed
If you need Web speed, you need Internet Server API (ISAPI) Extensions. An
ISAPI Extension DLL lets you replace the slow and resource-hogging Common
Gateway Interface (CGI) for such purposes as processing fill-in forms. As Part 1
of this series (October 1996) explained, CGI scripts and executables run in a
separate process space. ISAPI runs in the same process space as the HTTP server,
so ISAPI allows faster startup and communication than other types of CGI. In
addition to faster connections, ISAPI increases the speed of Microsoft's
Internet Information Server (IIS) because with ISAPI, IIS can use threading to
process more than one request at a time.
To create an ISAPI Extension, you can start from scratch or simply use the
Microsoft Foundation Class (MFC) ISAPI Wizard. If you choose MFC wizardry,
you'll finish very quickly. In addition, you can use the methodology this
article presents to create functional ISAPI Extension CGI replacements quickly
and consistently. You'll need IIS or another ISAPI HTTP server installed on a
Windows NT server, Microsoft Visual C++ 4.1 or later, and a CGI-compatible Web
browser, such as Internet Explorer or Netscape.
Part 1 showed how to create an ISAPI Extension that responds to a fill-in
form. The code did not do anything except display a default message. This
article shows how to modify an ISAPI Extension that the MFC Wizard generates. I
cover the Wizard-generated ISAPI Extension additions you need for the example
form in Part 1. The additions let you initially display the Comment and
Suggestion form shown in Screen 1 as the default and display the second page
with the return email address and link to the all-records page. The HTML that
produces the Comment and Suggestion fill-in form is in Listing 1.
ISAPI Wizardry Revisited
After you prepare the HTML for the fill-in form, as in Listing 1, create an
ISAPI Extension with the Visual C++ ISAPI Extension Wizard. The Wizard creates
an ISAPI Extension with the entire MFC ISAPI framework, but little else. We'll
extend this framework to display the Comment and Suggestion form shown in
Screen 1 and handle the CGI request generated when a user enters a comment (as you see in Screen 2) and clicks Send on the form.
To generate the initial ISAPI Wizard, start Visual C++, select the File
menu, and click New. Double-click the Project Workspace option.
The New Project Workspace dialog appears. From the Type list, select
ISAPI Extension Wizard. In the Name text box, type comment as the name of your
project. Click Create to pull up the ISAPI Extension Wizard - Step 1
of 1 dialog you see in Screen 3. Select Generate a Server Extension
object to specify that you want an MFC-generated class--an ISAPI
Extension--to handle ISAPI Extension, or CGI, requests. Click Finish to
display the New Project Information dialog. Click OK to complete
the task.
Modifying the MFC Wizard ISAPI Extension
So let's walk through a simple, standard methodology to finish the ISAPI
Extension created in Part 1 of this series. The ISAPI Extension will display the
Comment and Suggestion form for a URL that points to the ISAPI Extension with no
arguments (http://spain.winntmag.com/comment.dll?). You must add a method,
postComments, to the class created by the MFC Wizard, CCommentExtension, to
process the values passed to the ISAPI Extension DLL when someone selects Send
on this form. Listing 2 shows the prototype for the
CCommentExtension::postComments method.
The MFC ISAPI framework uses a CGI request-to-method parse map to determine
which method of the CCommentExtension class to call and how to map the
CGI-request named values to the method's parameters. To set up the parse map for
the comment and suggestion form, add the two lines in Listing 3 to the parse map
at the top of the comment.cpp file. The parse map, an MFC addition, maps the
parameters of a specific method of the CComment Extension class(e.g., LPTSTR
whoIsIt) to the named values (e.g., INPUT NAME= "WHOISIT") sent from
the fill-in form. The first parse map entry line, ON_ PARSE_COMMAND macro,
specifies the method name (postComments), the class (CCommentExtension), and the
data type of each of the class method's parameters. The second line specifies
the named values passed from the form to the ISAPI Extension. These values
correspond to the class method's parameters specified in the first macro. For
example, the first parameter for the postComments method is LPTSTRwhoIsIt. In
the first macro, ITS_PSTR (a string type) is the type for LPTSTR whoIsIt. In the
second macro, WHOISIT is the named value associated with this parameter. MFC
uses this parse map to associate incoming form data (named values) with
parameters to pass to class methods.
Before we add the postComments class method, we need to store and read long
strings of HTML code. More specifically, we want to store as a resource the
initial form and portions of the results page that the postComments method
generates. Visual C++ 4.1 includes the WWW QUOTE example, which illustrates how
to create a new custom resource type for HTML code. To simplify the process of
loading long HTML strings from resources, we'll borrow some code and techniques
highlighted in the WWW QUOTE example.
First, incorporate two class methods from this example into your ISAPI
Extension class: Copy the WritePage Title and LoadLongResource methods from the
WWWQUOTE example and change the class names from CWWWQuote to
CCommentExtension to match your class name in the method declarations and
definitions. You'll use this codelater in this example for reading whole HTML
pages from custom HTML resources. You can put this code into a separate file and
call it helper.cpp for future use in other ISAPI Extension projects.