Microsoft Docs

D2D1CreateFactory

Creates a Direct2D factory object that can be used to generate Direct2D resources and to manage drawing state.

Syntax

HRESULT D2D1CreateFactory(
    D2D1_FACTORY_TYPE factoryType,
    REFIID            riid,
    const D2D1_FACTORY_OPTIONS *pFactoryOptions,
    void              **ppIFactory
);

Parameters

  • factoryTypeD2D1_FACTORY_TYPE_SINGLE_THREADED or D2D1_FACTORY_TYPE_MULTI_THREADED.
  • riid – Interface identifier of the factory interface to retrieve (typically IID_ID2D1Factory).
  • pFactoryOptions – Optional pointer to a D2D1_FACTORY_OPTIONS structure; can be null.
  • ppIFactory – Receives the created factory interface pointer.

Return Value

The function returns S_OK on success. On failure, it returns a standard COM error code (e.g., E_OUTOFMEMORY).

Remarks

The factory created by D2D1CreateFactory is the entry point for all Direct2D objects. Use a single, globally shared factory wherever possible to reduce resource usage.

When using the multi‑threaded mode, all Direct2D objects created from the factory can be used across threads without additional synchronization.

Example

#include <d2d1.h>
#pragma comment(lib, "d2d1.lib")

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    ID2D1Factory *pFactory = nullptr;
    HRESULT hr = D2D1CreateFactory(
        D2D1_FACTORY_TYPE_SINGLE_THREADED,
        __uuidof(ID2D1Factory),
        nullptr,
        reinterpret_cast<void**>(&pFactory));

    if (SUCCEEDED(hr))
    {
        // Factory is ready for use
        // ...
        pFactory->Release();
    }
    return 0;
}

See Also