Win32 API Reference

OLE32 Functions

This section provides detailed documentation for functions exported by the OLE32 library. OLE (Object Linking and Embedding) is a technology that allows applications to share information and functionality by embedding objects from one application into documents of another.

Core OLE Functions

Object Management

COM Type Information


CoCreateInstance

Creates a single uninitialized object of the specified class. This function is the primary way to create COM objects in an application.

HRESULT CoCreateInstance(

   REFCLSID rclsid,

   LPUNKNOWN pUnkOuter,

   DWORD dwClsContext,

   REFIID riid,

   LPVOID *ppv

);

Parameters
Return Value

S_OK if the object was successfully created and initialized; otherwise, one of the error codes listed below.

Remarks

This function combines the functionality of CoGetClassObject, creating an object, and the QueryInterface call to obtain the specified interface pointer.

Example

#include <windows.h>
#include <ole2.h>

// Assume IMyObject and MY_OBJECT_CLSID are defined elsewhere

IMyObject *pMyObject = NULL;
HRESULT hr = CoCreateInstance(
    MY_OBJECT_CLSID,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_IMyObject,
    (void **)&pMyObject);

if (SUCCEEDED(hr)) {
    // Use pMyObject...
    pMyObject->Release();
}
            

CoInitializeEx

Initializes the COM library on the current thread and specifies the concurrency model for the thread.

HRESULT CoInitializeEx(

   LPVOID pvReserved,

   DWORD dwCoInit

);

Parameters
Return Value

S_OK if the COM library was successfully initialized for the current thread; S_FALSE if the COM library was already initialized for the current thread; otherwise, one of the error codes listed below.

Remarks

Every thread that uses the COM library must call CoInitializeEx once before calling any other COM functions. A thread can call CoInitializeEx multiple times, but each successful call must be matched by a corresponding call to CoUninitialize.

Example

#include <windows.h>
#include <ole2.h>

// Initialize COM for single-threaded apartment model
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

if (SUCCEEDED(hr)) {
    // Use COM services...
    CoUninitialize();
}
            

CoUninitialize

Closes the COM library on the current thread, unpacking all previously allocated resources for the thread.

void CoUninitialize(void);

Remarks

CoUninitialize is the counterpart to CoInitializeEx. Each call to CoInitializeEx must be balanced by a call to CoUninitialize. Failure to uninitialize the COM library can lead to resource leaks.

Example

#include <windows.h>
#include <ole2.h>

// ... Initialize COM ...
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

// ... Use COM services ...

// Uninitialize COM
CoUninitialize();
            
See Also

IUnknown Interface

The IUnknown interface is the base interface for all COM interfaces. It provides the core mechanisms for reference counting and querying for other interfaces.

Methods

QueryInterface

This method is part of the IUnknown interface. It retrieves pointers to the supported interfaces on an object.

HRESULT QueryInterface(

   REFIID riid,

   LPVOID *ppvObject

);

AddRef

This method is part of the IUnknown interface. It increments the reference count for the object, which ensures that the object remains alive until it is no longer needed.

ULONG AddRef(void);

Release

This method is part of the IUnknown interface. It decrements the reference count for the object. When the reference count reaches zero, the object is destroyed.

ULONG Release(void);

Related Topics