This sample demonstrates how to invoke a method provided by an COM component from within an ISAPI extension. Included in the project directory you will find getusername.dll, which is a simple ATL-based component which creates one class with a single method, GetMyName. This method, when called, will return the user name for the context in which the component is running.
Interaction with COM objects requires careful library management, and this sample performs two operations that allow the extension to function properly. First, the initial entry-point function GetExtensionVersion is used to initialize the COM Library with the CoInitialize method. This method must be called before any other COM-based operations. This sample also uses the #import directive to collect and incorporate type library information directly from the .dll file itself.
The HttpExtensionProc function is called by IIS next, and the real work of the extension begins. ServerSupportFunction is used to pass the HSE_REQ_SEND_RESPONSE_HEADER_EX extension request to the server, which takes care of sending the initial HTTP header to the client browser. Then the CreateInstance method, a wrapper for the COM library's CoCreateInstance method, is invoked to create an instance of the GetUserName object. The reference obtained from that method is used to access the GetMyName method of the component, and the output is written to the client browser.
To close the COM Library gracefully, it is important that any call to CoInitialize be balanced by its companion function, CoUninitialize. This sample extension makes use of the fact that IIS calls the entry-point function TerminateExtension as the last action before the extension is unloaded from memory. CoUninitialize is invoked from this final function, allowing the extension to terminate cleanly and efficiently.
Important The #import directive is supported by Visual C++ versions 5.0 or later. If your compiler does not support this directive, this sample will not compile and link properly.
This project is available in the ...isapi\extensions\com subdirectory of the IIS samples directory.