我在 C++ 中使用NVAPI来修改程序中的 NVIDIA 显示设置。
我无法成功使用该NvAPI_GPU_GetAllDisplayIds
功能。调用它返回的状态是NVAPI_INCOMPATIBLE_STRUCT_VERSION
。
这是我的代码:
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;
}
我使用malloc
不正确还是什么?谢谢!