ISAPI与CGI的比较及其实现
五、ISAPI的实现
能够支持ISAPI开发的平台只有微软的几个平台组合(如NT+IIS,WIN95+MSpersonalwebserver)。因此开发ISAPI只能这几个平台上。开发语言前已有述,这里向大家推荐VisualC++4.2版,它的Wizard提供了专门制作ISAPI的功能项。
下面是个简单的例子,功能是将用户从浏览器上的输入简单地回显给用户,功能简单,但是具备一个典型的ISAPI全部流程。源程序如下(大部分为Wizard生成的代码):
1.主程序:
//ISAPI1.CPP-ImplementationfileforyourInternetServer #include"stdafx.h" #include"isapi1.h" /////////////////////////////////////////////////////////////////////// //TheoneandonlyCWinAppobject //NOTE:Youmayremovethisobjectifyoualteryourprojecttono //longeruseMFCinaDLL. CWinApptheApp; /////////////////////////////////////////////////////////////////////// //command-parsingmap BEGIN_PARSE_MAP(CIsapi1Extension,CHttpServer) /*************Mycodesbeginhere***********/ ON_PARSE_COMMAND(RegisterUser, CIsapi1Extension,ITS_PSTRITS_PSTRITS_PSTR) ON_PARSE_COMMAND_PARAMS("FirstMiddleLast") /*************Mycodesendhere***********/ END_PARSE_MAP(CIsapi1Extension) /////////////////////////////////////////////////////////////////////// //TheoneandonlyCIsapi1Extensionobject CIsapi1ExtensiontheExtension; /////////////////////////////////////////////////////////////////////// //CIsapi1Extensionimplementation CIsapi1Extension::CIsapi1Extension() { } CIsapi1Extension::~CIsapi1Extension() { } BOOLCIsapi1Extension::GetExtensionVersion (HSE_VERSION_INFO*pVer) { //Calldefaultimplementationforinitialization CHttpServer::GetExtensionVersion(pVer); //Loaddescriptionstring TCHARsz[HSE_MAX_EXT_DLL_NAME_LEN+1]; ISAPIVERIFY(::LoadString(AfxGetResourceHandle(), IDS_SERVER,sz,HSE_MAX_EXT_DLL_NAME_LEN)); _tcscpy(pVer->lpszExtensionDesc,sz); returnTRUE; } /////////////////////////////////////////////////////////////////////// //CIsapi1Extensioncommandhandlers /******Mycodebeginshere**********/ voidCIsapi1Extension::RegisterUser (CHttpServerContext*pctxt,LPCTSTR pstrFirst,LPCTSTRpstrMiddle,LPCTSTRpstrLast) { //doprocessinghere! *pctxt<<_T("Yournameis:"); *pctxt<<_T(pstrFirst); *pctxt<<_T("\n"); *pctxt<<_T("YourAddressis:"); *pctxt<<_T(pstrMiddle); *pctxt<<_T("\n"); *pctxt<<_T("YourE_mailis:"); *pctxt<<_T(pstrLast); } /*********Mycodesendhere**********/ //Donoteditthefollowinglines, whichareneededbyClassWizard. #if0 BEGIN_MESSAGE_MAP(CIsapi1Extension,CHttpServer) //{{AFX_MSG_MAP(CIsapi1Extension) //}}AFX_MSG_MAP END_MESSAGE_MAP() #endif//0
2.主头文件
//ISAPI1.H-HeaderfileforyourInternetServer //isapi1Extension #include"resource.h" classCIsapi1Extension:publicCHttpServer { public: CIsapi1Extension(); ~CIsapi1Extension(); //Overrides //ClassWizardgeneratedvirtualfunctionoverrides //NOTE-theClassWizardwilladdandremovememberfunctionshere. //DONOTEDITwhatyouseeintheseblocksofgeneratedcode! //{{AFX_VIRTUAL(CIsapi1Extension) public: virtualBOOLGetExtensionVersion(HSE_VERSION_INFO*pVer); //}}AFX_VIRTUAL //TODO:Addhandlersforyourcommandshere. /***********Mycodedbeginhere(函数声明)*********/ voidRegisterUser(CHttpServerContext*pctxt, LPCTSTRpstrFirst,LPCTSTR pstrMiddle,LPCTSTRpstrLast); /*********Mycodesendhere DECLARE_PARSE_MAP() //{{AFX_MSG(CIsapi1Extension) //}}AFX_MSG };
3.动态连接库定义文件
/*************************************/ /*ISAPI1.def*/ /*************************************/ ;ISAPI1.def:declaresthemoduleparametersfortheDLL. LIBRARY"ISAPI1" EXPORTS HttpExtensionProc GetExtensionVersion
在上述程序中,定义了一个CHttpServer的类CIsapi1Extension,这是该程序的主要一个类,通过它实现与用户交互的功能。由ON_PARSE_COMMAND段实现函数映射,上述程序中定义了该DLL中的一个成员函数RegisterUser,其功能就是将用户在编辑框中输入的信息发送回用户。关于具体实现细节,可以参看VC4.2帮助文档。
将上述文件编译成DLL后放入服务器主机内WWW服务器指定的用户有执行权限的Scripts目录里,该目录的逻辑名应和交互主页里的一致。设置完后,通过浏览器连到服务器上,浏览该主页,并输入信息到编辑框中。
0
相关文章