问题
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.
回答1:
According to an answer in a Microsoft's forum a breaking change in the Windows SDK 8.0 requires you to define _WIN32_WINNT
to 0x0600
for backward compatibility with Windows Vista or to 0x0601
for backward compatibily with Windows 7.
回答2:
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);
}
I had use a C++11 lambda for this code in the past, but clang/LLVM doesn't like it so much.
回答3:
using this
#if defined(CLSID_WICImagingFactory)
#undef CLSID_WICImagingFactory
#endif
and then you may pass this
Refer: http://skia.googlecode.com/svn/trunk/src/ports/SkImageDecoder_WIC.cpp
来源:https://stackoverflow.com/questions/14799179/cocreateinstance-of-iwicimagingfactory