【问题标题】:WIC factory will always be nullptr on windows7 (used "What's a Creel?" tutorial)WIC 工厂在 windows7 上将始终为 nullptr(使用“什么是纱架?”教程)
【发布时间】:2019-11-25 20:11:52
【问题描述】:

我使用了this 教程,但总是收到一条错误消息,提示“wicFactory 为 nullptr”。我正在使用 Windows 7,但此代码无法正常工作。我读过你应该使用CLSID_WICImagingfactory1 但它不起作用。 这是我的代码:

HRESULT hr;
bmp = nullptr;

IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICFormatConverter *pConverter = NULL;

//Creating a factory
IWICImagingFactory *wicFactory = NULL;
CoCreateInstance(
    CLSID_WICImagingFactory1,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_IWICImagingFactory,
    (LPVOID*)&wicFactory);

MessageBox(NULL, (LPCWSTR)wicFactory, NULL, MB_OK);

//Creating a decoder

hr = wicFactory->CreateDecoderFromFilename(
    filename,
    NULL,
    GENERIC_READ,
    WICDecodeMetadataCacheOnLoad,
    &pDecoder);

//Read Frame from image
if (SUCCEEDED(hr)) {
    // Create the initial frame.
    hr = pDecoder->GetFrame(0, &pSource);
}

//Creating a Converter
if (SUCCEEDED(hr)) {

    // Convert the image format to 32bppPBGRA
    // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
    hr = wicFactory->CreateFormatConverter(&pConverter);
}

//Setup the converter
if (SUCCEEDED(hr)) {
    hr = pConverter->Initialize(
        pSource,
        GUID_WICPixelFormat32bppPBGRA,
        WICBitmapDitherTypeNone,
        NULL,
        0.0f,
        WICBitmapPaletteTypeMedianCut
    );
}
//Use the converter to create an D2D1Bitmap
//ID2D1Bitmap* bmp;
if (SUCCEEDED(hr))
{
    hr = d2dg->pRT->CreateBitmapFromWicBitmap(
        pConverter,
        NULL,
        &bmp
    );
}

if (wicFactory)wicFactory->Release();
if (pDecoder)pDecoder->Release();
if (pConverter)pConverter->Release();
if (pSource)pSource->Release();

谁能确定这里的问题?除了将 _WIN32_WINNT 定义为 0x0600 或 0x0601 之外,我找不到有关该问题的更多信息,但这仍然无济于事...

【问题讨论】:

  • 在创建工厂之前你会调用 CoInitializeEx 吗?
  • 感谢您的帮助
  • 是的,请记住,您应该在创建任何 COM 对象之前调用 CoInitializeEx。
  • 您应该检查 HRESULT 代码 CreateDecoderFromFilename 返回的内容。
  • 然后,你应该通过谷歌搜索来检查这个 hr 的含义。

标签: c++ null direct2d nullptr wic


【解决方案1】:

如 cmets 中所述,您需要先调用 CoInitializeEx,然后才能调用 CoCreateInstance

顺便说一句,这里有一些用于创建 WIC 工厂的强大代码,用于处理 Windows 7 的“WIC2”与“WIC1”下层场景,无论是否更新 KB2670838

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
        {
            g_WIC2 = false;

            hr = CoCreateInstance(
                CLSID_WICImagingFactory1,
                nullptr,
                CLSCTX_INPROC_SERVER,
                __uuidof(IWICImagingFactory),
                ifactory
            );
            return SUCCEEDED(hr) ? TRUE : FALSE;
        }
    #else
        g_WIC2 = false;

        return SUCCEEDED(CoCreateInstance(
            CLSID_WICImagingFactory,
            nullptr,
            CLSCTX_INPROC_SERVER,
            __uuidof(IWICImagingFactory),
            ifactory)) ? TRUE : FALSE;
    #endif
    }
}

IWICImagingFactory* GetWICFactory(bool& iswic2) 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;
    }

    iswic2 = g_WIC2;
    return factory;
}

有关 WIC2 中内容的详细信息,请参阅Microsoft Docs。在安装 WIC2 时,我支持 DirectXTex 以获得 32bpp BMP 和 alpha (BITMAPV5HEADER) 并支持 96bp TIFF (GUID_WICPixelFormat96bppRGBFloat)。

如果已安装,您可以在 Windows 7 上使用 WIC2,您可以通过使用 /D_WIN32_WINNT=0x0601 /D_WIN7_PLATFORM_UPDATE 构建来启用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 2020-02-04
    • 2015-05-30
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多