Microsoft KB Archive/289112

= How To Initialize a COM Apartment in an ISAPI Extension or Filter =

Article ID: 289112

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Internet Information Server 4.0
 * Microsoft Internet Information Services 5.0

-



This article was previously published under Q289112



We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:

http://www.microsoft.com/technet/security/prodtech/IIS.mspx



SUMMARY
This article describes how to initialize a Component Object Model (COM) apartment in an Internet Server API (ISAPI) extension or filter. To use a COM component in an ISAPI filter, you need to fully understand how COM apartments and ISAPI thread pools interact.



MORE INFORMATION
Internet Information Server (IIS) is a multithreaded application, and the ISAPI extension is called on different threads for each request. When a threading model mismatch occurs, COM creates the component in a thread of its own. This thread inherits the credentials of the calling process, which is InetInfo.exe. To correctly initialize the desired COM apartment in an ISAPI extension or filter, you must create a separate thread and initialize that thread with the required apartment.

ASP threads are initialized as apartment threads in both the IIS 4.0 and the IIS 5.0 threading models. However, to use COM in ISAPI, you should not initialize COM or make a COM call in the GetExtensionVersion function unless you require that these calls be made under the credentials of either the SYSTEM account (for low-security ISAPI DLLs) or the IWAM_machinename account (for medium- or high-security ISAPI DLLs).

You can use COM in ISAPI in the following two ways:
 * Create your own thread or a thread pool in an ISAPI DLL and initialize the thread (or each thread in the pool) to match the threading model of the COM component. Then use this thread (or a thread pool) to instantiate and call the component.

NOTE: Microsoft recommends that you use this procedure.
 * In HttpExensionProc, call the CoInitializeEx(NULL, COINIT_MULTITHREADED) function and verify that this succeeds. If CoInitializeEx is successful, you can create, call, or release the COM component, and then call CoUninitialize prior to returning.

NOTE: Microsoft does not recommend that you use this procedure.

