A Sip From The Firehose: Monday, September 30, 1996

By: David Intersimone

Abstract: Make Big Bucks writing Web Server extension DLLs— A few strcats is all you need!

Monday, September 30, 1996
Scotts Valley, CA

Make Big Bucks writing Web Server extension DLLs—
A few strcats is all you need!

With all the hype about the Internet and how much money programmers can make setting up web sites for companies who can't do it themselves, I wanted to see what it takes to extend the Microsoft Internet Information Server (IIS). After reading an HTML document on Microsoft's web site, I realized that programmers are charging a lot for such simple programming. What does it take to build extension DLLs for IIS? With the Information Server Application Programming Interface (ISAPI), a beginning computer science student in college can easily build a DLL and charge large dollars per hour for the effort. With Delphi, Borland C++ or any other developer tool that can compile and link DLLs for Windows NT you can do it too and be on the road to vast wealth as an Internet Server programmer. Here's how!

First, read the Internet Server API Overview document on Microsoft's web site and the Internet Server API Reference document. These documents will explain what ISAPI is, what you can build with the API, and how it compares to the Common Gateway Interface (CGI) standard for web server programming. Basically the difference is CGI uses and EXE file and ISAPI programming uses a DLL. CGI executables run as a separate process, ISAPI DLLs run in the same process as the IIS gaining several benefits including performance.

In this weeks Sip I will show you how to write a simple ISAPI DLL in C++. For more information about using Delphi for Internet programming check our the the paper by Charlie Calvert, a member of the Borland Developer Relations department, that was presented at the Seventh Annual Borland Developers Conference last July that showed how you can use Delphi and the Internet to build ISAPI DLLs.

Second, make sure you have the httpext.h header file (available as part of Microsoft's ActiveX SDK). This header file contains the defines, typedefs and function prototypes for building ISAPI DLLs. The key structure involved in ISAPI programming is the Extension Control Block (ECB). The ECB contains the parameters that were included (if there were any) with the URL, IIS functions you can call , bytes sent by the client, the content type from the client and other information.

Third, write a simple ISAPI extension DLL you have to provide two functions: GetExtensionVersion and HttpExtensionProc. When the IIS sees a DLL as part of the URL, the web server loads the DLL into memory and calls the GetExtensionVersion function to determine which version of ISAPI you are using and then calls the HttpExtensionProc that does the work required for your application. The ISAPI1.CPP source code shows how to echo the parameter string back the client browser or program. The program is nothing but a series of strcpy and strcat function calls. Of course the complexity of the ISAPI DLL depends on your application. Compile and link the source code to build the DLL. Copy it over to your IIS in the directory where you place your extension DLLs. (Note: if you've already tried the DLL it is probably still in memory. You will need to stop the web server to release the DLL and then start the web server again. Use the IIS manager application to start and stop it). I have also included the ISAPI1.DEF file that defines the exported functions defined in the DLL.


ISAPI1.CPP:
// Simple parameter ISAPI extension DLL.  Echoes the parameter string back to the client.
#include <windows.h>
#include "httpext.h"
#include <string.h>
#include <stdio.h>

// GetExtensionVersion callback definition
BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
  pVer->dwExtensionVersion = MAKELONG(HSE_VERSION_MINOR, HSE_VERSION_MAJOR);
  strcpy(pVer->lpszExtensionDesc,"Simple Parameter ISAPI DLL");
  return (TRUE);   // I can't imagine how this function can return anything but true
};

//HttpExtensionProc callback definition
DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *pECB) {
  char HtmlString[500];
  char FinalString[1024];
  DWORD FinalStringLength;

  char *HtmlOpen = "<HTML>";
  char *HtmlClose = "</HTML>";
  char *MyTitle =
   "<HEAD><TITLE>Borland C++ Simple Parameter ISAPI Extension DLL Result Document</TITLE></HEAD>";
  char *MyHtmlBodyOpen = "<BODY><H1>ISAPI1 Parameter was: </H1>";
  char *MyHtmlBodyClose = "<BR></BODY>";
  char *IsapiLogText = "ISAPI1 - Simple Parameter BC++ ISAPI Extension DLL";

  strcpy(pECB->lpszLogData,IsapiLogText);
  pECB->dwHttpStatusCode = 200;   // Code 200 says that the request completed successfully

  // build the HTML result string
  strcpy(HtmlString,HtmlOpen);
  strcat(HtmlString,MyTitle);
  strcat(HtmlString,MyHtmlBodyOpen);

  // if there is a parameter string echo it back
  if(lstrlen(pECB->lpszQueryString)==0)
    strcat(HtmlString,"*** NO PARAMETER GIVEN ***");
  else
    strcat(HtmlString,pECB->lpszQueryString);

  strcat(HtmlString,MyHtmlBodyClose);
  strcat(HtmlString,HtmlClose);

  //  Build the final result string to be sent back to the browser.
  //  The string contains the HTTP protocol version number (HTTP 1.0)
  //    and the Server status code of 200 for successful operation.
  sprintf(FinalString,
    "HTTP/1.0 200 OKnContent-Type: text/htmlnContent-Length: %dnContent:nn %s",
    strlen(HtmlString), &HtmlString);

  //  get the final length of the whole string - HTTP header + the generated HTML
  FinalStringLength = lstrlen(FinalString);

  //  use the ECB to call WriteClient and pass the final string and length
  pECB->WriteClient(pECB->ConnID, FinalString, &FinalStringLength, 0);

  return (HSE_STATUS_SUCCESS);   // return a successful status back to IIS
}

ISAPI1.DEF:
LIBRARY ISAPI1
DESCRIPTION 'A Simple ISAPI Extension DLL'
EXPORTS
  HttpExtensionProc
  GetExtensionVersion

Fourth, create a sample HTML document, place it on your IIS and access it from your favorite browser. The HTML should look something like:

<HTML>
<HEAD>
<TITLE>My Home Page</TITLE>
</HEAD>
<BODY>
<H1>My Home Page Documents</H1>
<P>
This is the home page for my home computer.
<P>
<A HREF="http://www.borland.com/scripts/mystuff/isapi1.dll?HelloWorld">ISAPI One</A><BR>
</BODY>
</HTML>

You can now write your way to untold riches with a few lines of code, some strcpys and strcats, and of course a little programming magic thrown in.

Good luck with your ISAPI programming.

David I
(davidi@inprise.com)


Server Response from: ETNASC03