CreateWICTextureFromFile返回E_NOINTERFACE

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

我想创建一个纹理加载器。但是,当我创建以下行时:

HRESULT res = CreateWICTextureFromFile(device, file.c_str(),&resource,&shaderResourceView,0);

它返回E_NOINTERFACE。我要加载的文件明确存在,并且ID3D11DeviceID3D11DeviceContext均已成功创建。

这是我的课堂宣言:

    using std::wstring;
    using Microsoft::WRL::ComPtr;
    using DirectX::CreateWICTextureFromFile;
    class Texture2DHandle {
    private:
        ComPtr<ID3D11Texture2D> texture;
        ComPtr<ID3D11ShaderResourceView> shaderResourceView;
    public:
        Texture2DHandle() = default;
        Texture2DHandle(void* data, int w, int h, DXGI_FORMAT format, ID3D11Device* device);
        Texture2DHandle(wstring file, ID3D11Device* device, ID3D11DeviceContext* context);

        ID3D11Texture2D* tex() const;
        ID3D11ShaderResourceView* srv() const;
    };

这是构造函数的定义:

Texture2DHandle::Texture2DHandle(wstring file, ID3D11Device* device,ID3D11DeviceContext* context)
{
    ComPtr<ID3D11Resource> resource;
    HRESULT res = CreateWICTextureFromFile(device, file.c_str(),&resource,&shaderResourceView,0);
    if (FAILED(res))
        throw std::exception("Could not load texture from file!");
    res = resource.As<ID3D11Texture2D>(&texture);
    if (FAILED(res))
        throw std::exception("Could not load texture from file!");
}

由于Device和DeviceContext都存在,所以我不知道E_NOINTERFACE的含义

c++ com direct3d hresult wic
1个回答
0
投票

确保已初始化COM,因为这是最可能的问题。

#if (_WIN32_WINNT >= 0x0A00 /*_WIN32_WINNT_WIN10*/)
    Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
    if (FAILED(initialize))
        // error
#else
    HRESULT hr = CoInitializeEx(nullptr, COINITBASE_MULTITHREADED);
    if (FAILED(hr))
        // error
#endif

请参见WICTextureLoader以获取详细文档。

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