CoEnterServiceDomain function (comsvcs.h)

Used to enter code that can then use COM+ services.


HRESULT CoEnterServiceDomain(
  [in] IUnknown *pConfigObject


[in] pConfigObject

A pointer to the IUnknown interface of the object, created from the CServiceConfig class, that contains the configuration information for the services to be used within the enclosed code.

Return value

This method can return the standard return values E_INVALIDARG, E_OUTOFMEMORY, and E_FAIL, as well as the following values.

Return code Description
The method completed successfully.
The side-by-side assembly configuration of the CServiceConfig object is invalid.
The thread pool configuration of the CServiceConfig object is invalid. The thread apartment model cannot be reconfigured by calling CoEnterServiceDomain.
The tracker configuration of the CServiceConfig object is invalid.
The caller does not have access permissions for the COM+ partition.


Code that is enclosed between calls to CoEnterServiceDomain and CoLeaveServiceDomain runs in its own context and behaves as though it were a method that is called on an object created within the context. CoEnterServiceDomain cannot switch to a different apartment model, so the enclosed code runs in the caller's apartment and on the caller's thread. It is an error to try to change the apartment model through the CServiceConfig object when calling CoEnterServiceDomain.

CoEnterServiceDomain first creates a context that is configured as specified by the CServiceConfig object that is passed as the pConfigObject parameter. Policies on both the client and server sides are then triggered as if a method call had occurred. The new context is then pushed onto a context stack and becomes the current context.

Because of their efficient design and because no thread marshaling is involved, using CoEnterServiceDomain and CoLeaveServiceDomain involves significantly reduced overhead as compared to an equivalent method call.

CoEnterServiceDomain and CoLeaveServiceDomain are particularly useful in applications, which can use these functions to access COM+ services without needing to create a component to do so.

The CoEnterServiceDomain and CoLeaveServiceDomain pairs can be nested.


Minimum supported client Windows XP [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Target Platform Windows
Header comsvcs.h
Library ComSvcs.lib
DLL ComSvcs.dll

See also

COM+ Services Without Components