A simple page counter (ISAPI extension)

Here is a simple example using an ISAPI extension which creates a simple page counter. It is written in C.

/*
 *
 * A simple page counter
 *
 * This ISAPI extension writes out the number of times it has been accessed.
 *    NOTE: It should not be run out-of-process, as several threads might
 *          try to access the counter variable at the same time.
 *
 */

/* Include ISAPI definitions and constants */
#include <httpext.h>

/*  This variable holds the number of times this page has been accessed */
static int hits = 0;

/* This function is called when the extension is loaded by the web server */
BOOL WINAPI
GetExtensionVersion( HSE_VERSION_INFO *pVer )
{
  /*  Set the extension version */
  pVer->dwExtensionVersion = HSE_VERSION;
  /*  Set a description string for the extension */
  strncpy( pVer->lpszExtensionDesc, "A Simple Page Counter",
	   HSE_MAX_EXT_DLL_NAME_LEN );
  return TRUE;
}

/*  This function is called when the extension is accessed */
DWORD WINAPI
HttpExtensionProc( LPEXTENSION_CONTROL_BLOCK ecb )
{
  char *header   = "Content-Type: text/plain";
  int headerlen  = strlen( header );
  char msg[256];
  int msglen;

  /* Use a server support function to write out a header with our
     additional header information */
  ecb->ServerSupportFunction( ecb->ConnID, HSE_REQ_SEND_RESPONSE_HEADER, 0,
			      &headerlen, (DWORD *)header );

  /* Write out the number of accesses */
  sprintf( msg, "This page has been accessed %d times", ++hits );
  msglen = strlen( msg );
  ecb->WriteClient( ecb->ConnID, msg, &msglen, 0 );

  /* Return, indicating success */
  return HSE_STATUS_SUCCESS;
}
Content Manager [Administrator] 16 December 2005  Permalink  
Download Free Trial

Recent Articles

Other Resources



www.zeus.com