Search code examples
visual-c++mfcdirectxdxgi

What is the optimal way to fetch graphics card name and info in a Windows C++ app?


I'm trying to pull some information from client machines that I'd like to be formatted almost exactly as it's seen here from "dxdiag.exe": enter image description here

I realize there should be an API for this type of functionality, but I've searched and searched and can't figure out library or header file I need to include to access this tool. Any help is greatly appreciated.


Solution

  • For Windows Vista or later, use DXGI.

    #include <dxgi.h>
    #include <wrl/client.h>
    
    #pragma comment(lib, "dxgi.lib")
    
    using Microsoft::WRL::ComPtr;
    
    ComPtr<IDXGIFactory1> dxgiFactory;
    HRESULT hr = CreateDXGIFactory1(IID_PPV_ARGS(dxgiFactory.ReleaseAndGetAddressOf()));
    if (FAILED(hr)) // ... error handling
    
    ComPtr<IDXGIAdapter1> adapter;
    for (UINT adapterIndex = 0;
         SUCCEEDED(dxgiFactory->EnumAdapters1(
                   adapterIndex,
                   adapter.ReleaseAndGetAddressOf()));
         adapterIndex++)
    {
        DXGI_ADAPTER_DESC1 desc = {};
        hr = adapter->GetDesc1(&desc);
        if (FAILED(hr)) // ... error handling
    
        if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
        {
            // Don't select the Basic Render Driver adapter.
            continue;
        }
    
        // desc.VendorId: VID
        // desc.DeviceId: PID
        // desc.Description: name string seen above
    }
    

    You can also look at the source code for DirectX Capabilities Viewer and the sample SystemInfoUWP.

    I used Microsoft::WRL::ComPtr as a C++ smart-pointer for COM.