1

I'm using NVAPI in C++ to modify NVIDIA display settings in my program.

I cannot successfully use the NvAPI_GPU_GetAllDisplayIds function. The status returned from calling it is NVAPI_INCOMPATIBLE_STRUCT_VERSION.

Here is my code:

int main() {
    NvAPI_Status status;

    NvPhysicalGpuHandle nvGPUHandle[64];
    NvU32 gpuCount;
    status = NvAPI_EnumPhysicalGPUs(nvGPUHandle, &gpuCount);
    if (NVAPI_OK != status) {
        cerr << "Failed to run function: NvAPI_EnumPhysicalGPUs\nStatus: " << status << endl;
        return 1;
    }
    if (gpuCount <= 0) {
        cerr << "No GPU's found" << endl;
        return 1;
    }

    for (unsigned i = 0; i < gpuCount; ++i) {
        const NvPhysicalGpuHandle& hPhysicalGpu = nvGPUHandle[i];

        NvU32 displayIdCount = 0;
        status = NvAPI_GPU_GetAllDisplayIds(hPhysicalGpu, nullptr, &displayIdCount);
        if (NVAPI_OK != status) {
            cerr << "Failed to run function: NvAPI_GPU_GetAllDisplayIds\nStatus: " << status << endl;
            return 1;
        }
        if (displayIdCount <= 0) {
            cerr << "No display's found" << endl;
            return 1;
        }

        NV_GPU_DISPLAYIDS* displayIds = static_cast<NV_GPU_DISPLAYIDS*>(malloc(sizeof(NV_GPU_DISPLAYIDS) * displayIdCount));
        status = NvAPI_GPU_GetAllDisplayIds(hPhysicalGpu, displayIds, &displayIdCount);
        if (NVAPI_OK != status) {
            // status is NVAPI_INCOMPATIBLE_STRUCT_VERSION (-9)
            cerr << "Failed to run function: NvAPI_GPU_GetAllDisplayIds\nStatus: " << status << endl;
            return 1;
        }
    }

    return 0;
}

Am I using malloc incorrectly or something? Thank you!

Ari Seyhun
  • 11,506
  • 16
  • 62
  • 109

1 Answers1

2

This is not documented directly in the NVAPI documentation page for that function, but you need to set the version on your malloc'ed displayIds structure before passing it in to NvAPI_GPU_GetAllDisplayIds. Add this line before the call:

displayIds->version = NV_GPU_DISPLAYIDS_VER;

This seems to be pretty standard throughout the NVAPI with other function calls as well.

Marius
  • 296
  • 1
  • 3
  • Is what I'm attempting possible? I've searched the source code of NvAPI for words like `contrast`, `gamma`, `brightness` and have found no functions with the ability to change these simple settings. Maybe I'm confused as to what NvAPI actually is meant to do. – Ari Seyhun Jan 08 '18 at 14:48
  • Best Google result I found for this is https://devtalk.nvidia.com/default/topic/543255/nvapi-set-brightness-contrast-hue-saturation/ . Apparently it might be available in the (paid) version of the NVAPI. – Marius Jan 08 '18 at 15:52
  • 1
    You should be canonised man :D On the other hand the guy who wrote the docs... – CJ_Notned Apr 25 '22 at 09:05