Search code examples
cdxgi

How to call EnumAdapters from IDXGFactory in c?


How to call the EnumAdapters function in c from IDXGFactory

    UINT i = 0; 
    IDXGIFactory* pFactory = NULL;
    IDXGIAdapter * pAdapter; 
    HRESULT hr = CreateDXGIFactory(&IID_IDXGIFactory, (void**)(&pFactory) );

    if (hr != S_OK)
    {
        printf("Failed to create DXIFactory object\n");
    }

using pFactory->EnumAdapters(i, pAdapter) does not work and causes this error

struct "IDXGIFactory" has no field "EnumAdapters"

Solution

  • You are interested in reading up on topic "using COM in plain C", where you can find relevant detailed explanations.

    For you very specific question the code might look like:

    #include <dxgi.h>
    
    #pragma comment(lib, "dxgi.lib")
    #pragma comment(lib, "dxguid.lib")
    
    int main()
    {
        IDXGIFactory* pFactory = NULL;
        UINT i = 0; 
        IDXGIAdapter* pAdapter; 
        HRESULT hr;
        hr = CreateDXGIFactory(&IID_IDXGIFactory, (void**)(&pFactory));
        hr = pFactory->lpVtbl->EnumAdapters(pFactory, i, &pAdapter);
        return 0;
    }
    

    Or, another way is to take advantage of COBJMACROS in which case you have IDXGIFactory_EnumAdapters available to you.

    #define COBJMACROS
    
    #include <dxgi.h>
    
    #pragma comment(lib, "dxgi.lib")
    #pragma comment(lib, "dxguid.lib")
    
    int main()
    {
        IDXGIFactory* pFactory = NULL;
        UINT i = 0; 
        IDXGIAdapter* pAdapter; 
        HRESULT hr;
        hr = CreateDXGIFactory(&IID_IDXGIFactory, (void**)(&pFactory));
        hr = IDXGIFactory_EnumAdapters(pFactory, i, &pAdapter);
        return 0;
    }