CoCreateIWICImagingFactory的实例

问题描述 投票:0回答:4

我在 Windows 7 计算机上运行 Visual Studio 2012。

当我运行此处找到的 SimpleDirect2dApplication 时:http://technet.microsoft.com/en-us/subscriptions/dd940321%28v=vs.85%29.aspx

    hr = CoCreateInstance(
    CLSID_WICImagingFactory,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_PPV_ARGS(&m_pWICFactory)
    );

CoCreateInstance 失败并显示“类未注册”,工厂的 ptr 为 0。

如有任何帮助,我们将不胜感激。

c++ directx direct2d
4个回答
1
投票

根据 Microsoft 论坛中的 answer,Windows SDK 8.0 中的重大更改要求您将

_WIN32_WINNT
定义为
0x0600
以向后兼容 Windows Vista,或定义为
0x0601
以向后兼容 Windows 7。


0
投票

这是我用于处理 WIC 和 WIC2 场景的 WIC 创建的代码:

namespace
{
    bool g_WIC2 = false;

    BOOL WINAPI InitializeWICFactory(PINIT_ONCE, PVOID, PVOID *ifactory) noexcept
    {
    #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE)
        HRESULT hr = CoCreateInstance(
            CLSID_WICImagingFactory2,
            nullptr,
            CLSCTX_INPROC_SERVER,
            __uuidof(IWICImagingFactory2),
            ifactory
        );

        if (SUCCEEDED(hr))
        {
            // WIC2 is available on Windows 10, Windows 8.x, and Windows 7 SP1 with KB 2670838 installed
            g_WIC2 = true;
            return TRUE;
        }
        else
        {
            hr = CoCreateInstance(
                CLSID_WICImagingFactory1,
                nullptr,
                CLSCTX_INPROC_SERVER,
                __uuidof(IWICImagingFactory),
                ifactory
            );
            return SUCCEEDED(hr) ? TRUE : FALSE;
        }
    #else
        return SUCCEEDED(CoCreateInstance(
            CLSID_WICImagingFactory,
            nullptr,
            CLSCTX_INPROC_SERVER,
            __uuidof(IWICImagingFactory),
            ifactory)) ? TRUE : FALSE;
    #endif
    }
}

bool IsWIC2() noexcept
{
    return g_WIC2;
}

IWICImagingFactory* GetWIC() noexcept
{
    static INIT_ONCE s_initOnce = INIT_ONCE_STATIC_INIT;

    IWICImagingFactory* factory = nullptr;
    if (!InitOnceExecuteOnce(
        &s_initOnce,
        InitializeWICFactory,
        nullptr,
        reinterpret_cast<LPVOID*>(&factory)))
    {
        return nullptr;
    }
    return factory;
}

这可以处理从任何线程创建一次工厂。您只需拨打电话即可使用它:

auto pWIC = GetWIC();
if (!pWIC)
    // error

对于您关心 WIC 与 WIC2 的少数情况,您可以使用

IsWIC2
:

if (targetFormat && memcmp(&guidContainerFormat, &GUID_ContainerFormatBmp, sizeof(WICPixelFormatGUID)) == 0 && IsWIC2())
{
    // Opt-in to the WIC2 support for writing 32-bit Windows BMP files with an alpha channel
    PROPBAG2 option = {};
    option.pstrName = const_cast<wchar_t*>(L"EnableV5Header32bppBGRA");

    VARIANT varValue;
    varValue.vt = VT_BOOL;
    varValue.boolVal = VARIANT_TRUE;
    (void)props->Write(1, &option, &varValue);
}

我过去曾在这段代码中使用过 C++11 lambda,但 clang/LLVM 不太喜欢它。



-2
投票

使用这个

#if defined(CLSID_WICImagingFactory)
#undef CLSID_WICImagingFactory
#endif

然后你就可以通过这个

参考: http://skia.googlecode.com/svn/trunk/src/ports/SkImageDecoder_WIC.cpp

© www.soinside.com 2019 - 2024. All rights reserved.