CoGetMalloc

HRESULT CoGetMalloc(dwMemContext, ppMalloc)

This function retrieves from the COM library either the task memory allocator an optionally-provided shared memory allocator. The particular allocator of interest is indicated by the dwMemContext parameter. Legal values for this parameter are taken from the enumeration MEMCTX:


typedef enum tagMEMCTX {
        MEMCTX_TASK = 1,            // task (private) memory
        MEMCTX_SHARED = 2,          // shared memory (between processes)
        MEMCTX_MACSYSTEM = 3,       // on the mac, the system heap
        // these are mostly for internal use...
        MEMCTX_UNKNOWN = -1,        // unknown context (when asked about it)
        MEMCTX_SAME = -2,           // same context (as some other pointer)
    } MEMCTX;

MEMCTX_TASK returns the task allocator. If CoInitialize has not yet been called, NULL we be stored in ppMalloc and CO_E_NOTINITIALIZED returned from the function.

MEMCTX_SHARED returns an optionally-provided shared allocator; if the shared allocator is not supported, E_INVALIDARG is returned. When supported, the shared allocator returned by this function is an COM-provided implementation of IMalloc interface, one which allocates memory in such a way that it can be accessed by other process on the current machine simply by conveying the pointer to said applications.2. Further, memory allocated by this shared allocator in one application may be freed by the shared allocator in another. Except when a NULL pointer is passed, the shared memory allocator never answers -1 to IMalloc::DidAlloc; it always indicates that either did or did not allocate the passed pointer.

Argument

Type

Description

dwMemContext

DWORD

A value from the enumeration MEMCTX.

ppMalloc

IMalloc **

The place in which the memory allocator should be returned.


Return Value

Meaning

S_OK

Success. The requested allocator was returned.

CO_E_NOTINITIALIZED

The COM library has not been initialized.

E_INVALIDARG

An invalid argument was passed.

E_UNEXPECTED

An unknown error occurred.