Raildex
Raildex

Reputation: 4727

CreateWICTextureFromFile returns E_NOINTERFACE

I want to create a Texture loader. However, when I create the following line:

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

it returns E_NOINTERFACE. The file I want to load definitively exists and both the ID3D11Device and the ID3D11DeviceContext are created successfully.

Here is my class declaration:

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

And here is the definition of the constructor:

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!");
}

I dont have any clue what is meant with E_NOINTERFACE since both Device and DeviceContext exist

Upvotes: 2

Views: 710

Answers (2)

Mohit Dharmadhikari
Mohit Dharmadhikari

Reputation: 4100

While Calling CoInitializeEx is always best Practice. I believe you're working on Windows Desktop application (exe) so if you call

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

After ShowWindow() it should work without any error. However, recommended approach is always initialize the COM.

Upvotes: 0

Chuck Walbourn
Chuck Walbourn

Reputation: 41002

Make sure you have initialized COM as that's the most likely problem.

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

See WICTextureLoader for detailed docs.

Upvotes: 4

Related Questions