无法在direct2d项目中初始化IWICImagingFactory

时间:2015-03-18 14:50:22

标签: c++ direct2d

我正在创建一个direct2d项目,我无法加载位图图片,因为类IWICImagingFactory的对象为null。我不知道我是否正确调用CoCreateInstance函数。

这是我用来初始化所有内容的代码:

// ... d2dFactory ... and others objects
IWICImagingFactory* pIWICFactory = 0;

HRESULT TDE_Device::Initialize(HWND hwnd)
{
    HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &d2dFactory);
    //
    RECT rc;
    GetClientRect(hwnd, &rc);
    //
    D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);
    d2dFactory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(),     D2D1::HwndRenderTargetProperties(hwnd, size), &MainD2DCore.d2dRenderTarg);
    wndHandle = hwnd;
    //
    // ----------  HERE IS THE CoCreateInstance ------------------------
    CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, reinterpret_cast<void **>(&pIWICFactory));
    if (pIWICFactory == 0)
    {
        // I GET THIS MESSAGE
        MessageBox(NULL, L"Error creating the Image Factory", L"ERROR", MB_OK);
        return E_FAIL;
    }
    //
    return hr;
}

当我尝试使用此IWICImagingFactory从文件加载图片时,我得到了访问冲突异常,因为它正在读取地址0x0。

调用CoCreateInstance函数的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

是的,这是创建ImagingFactory的正确方法 你有没有打电话给CoInitializeEx(NULL,COINIT_MULTITHREADED);打电话给CoCreateInstance?