CoRegisterInitializeSpy function (objbase.h)

Registers an implementation of the IInitializeSpy interface. The IInitializeSpy interface is defied to allow developers to perform initialization and cleanup on COM apartments.


HRESULT CoRegisterInitializeSpy(
  [in]  IInitializeSpy *pSpy,
  [out] ULARGE_INTEGER *puliCookie


[in] pSpy

A pointer to an instance of the IInitializeSpy implementation.

[out] puliCookie

The address at which to store a cookie that identifies this registration.

Return value

This function can return the standard return value E_INVALIDARG, as well as the following values.

Return code Description
The object was successfully registered.
The object does not support IInitializeSpy.


The CoRegisterInitializeSpy function registers an implementation of the IInitializeSpy interface, which defines methods to be called when CoInitializeEx (or CoInitialize) or CoUninitialize is invoked.

CoRegisterInitializeSpy calls QueryInterface for IID_InitializeSpy on pSpy. It stores the address of the returned interface pointer in thread-specific storage that is independent of the COM initialization state for this thread. On success, it stores in puliCookie a ULARGE_INTEGER cookie that represents this registration. Pass this cookie to CoRevokeInitializeSpy to revoke the registration.

IInitializeSpy implementations must deal with nesting issues caused by calling CoInitializeEx or CoUninitialize from within a notification method. Notifications occur only after the registration happens on this thread. For example, if CoInitializeEx is called before CoRegisterInitializeSpy, then the PreInitialize and PostInitialize notification methods will not be called.

Notification methods must not cause the failure of CoInitializeEx or CoUninitialize by throwing exceptions. Implementations of IInitializeSpy must not propagate exceptions to code that calls CoInitializeEx or CoUninitialize.

It is unpredictable whether a call to CoRegisterInitializeSpy from within an IInitializeSpy method call will be effective during the current top-level (non-nested) call to CoInitializeEx or CoUninitialize. A registered implementation of IInitializeSpy will always be effective for future top-level calls to CoInitializeEx or CoUninitialize.


Requirement Value
Minimum supported client Windows XP with SP1 [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Target Platform Windows
Header objbase.h
Library Ole32.lib
DLL Ole32.dll

See also