本文介绍了IWICImagingFactory的CoCreateInstance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



当我运行在此处找到的SimpleDirect2dApplication时:

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

CoCreateInstance失败,并显示未注册类,并且工厂的ptr为0。 p>

任何帮助将不胜感激。

解决方案

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

 命名空间
{
bool g_WIC2 = false;

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

if(SUCCEEDED(hr))
{
// WIC2在Windows 10,Windows 8.x和Windows 7 SP1上可用,并已安装KB 2670838
g_WIC2 = true;
返回TRUE;
}
else
{
hr = CoCreateInstance(
CLSID_WICImagingFactory1,
nullptr,
CLSCTX_INPROC_SERVER,
__uuidof(IWICImagingFactory),
工厂
);
返回SUCCEEDED(hr)吗?真假;
}
#else
return SUCCEEDED(CoCreateInstance(
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
__uuidof(IWICImagingFactory),
工厂))?真假;
#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;
}
退货工厂;
}

此操作可以从任何线程创建一次工厂。您可以通过以下方式使用它:

  auto pWIC = GetWIC(); 
if(!pWIC)
//错误

在某些情况下,如果您关心WIC与WIC2,则使用 IsWIC2

  (targetFormat&& memcmp(& guidContainerFormat,& GUID_ContainerFormatBmp,sizeof(WICPixelFormatGUID))== 0&& IsWIC2())
{
//选择加入WIC2支持用于通过alpha通道
PROPBAG2 option = {}编写32位Windows BMP文件。
option.pstrName = const_cast< wchar_t *>(L EnableV5Header32bppBGRA);

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




I am running Visual Studio 2012 on my Windows 7 machine.

When I run the SimpleDirect2dApplication found here : 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)
    );

the CoCreateInstance fails with a "Class Not Registered" and the ptr to the factory is 0.

Any help would be appreciated.

解决方案

Here's the code I use to handle WIC creation for both WIC and WIC2 scenarios:

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;
}

This handles creating the factory once from any thread. You use it by just calling:

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

For those few cases where you care about WIC vs. WIC2, you use 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);
}

这篇关于IWICImagingFactory的CoCreateInstance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-04 23:41