使用NVAPI时,我在使用NvAPI_DISP_GetDisplayConfig时遇到问题。我在第二次调用NvAPI_DISP_GetDisplayConfig时收到AppCrash。
似乎无法找出原因。

NvU32 count = 0;
status = NvAPI_DISP_GetDisplayConfig(&count, NULL);
if (status != NVAPI_OK)
    PrintError(status);
printf("Configs: %i\n", count);
NV_DISPLAYCONFIG_PATH_INFO *configinfos = new NV_DISPLAYCONFIG_PATH_INFO[count];
configinfos[0].version = NV_DISPLAYCONFIG_PATH_INFO_VER;
status = NvAPI_DISP_GetDisplayConfig(&count, configinfos);
if (status != NVAPI_OK)
    PrintError(status);

在我的系统上,第一次调用后计数= 2。
关于NvAPI_DISP_GetDisplayConfig的注释说:



谢谢。

编辑:我也试图将configinfos [0] .sourceModeInfo = NULL设置为无效。我还尝试遍历数组以将所有.version和.sourceModeInfo设置为无效(在文档中的示例中,我仅在数组的第一项中看到了设置版本)

最佳答案

这应该为您工作:

NvAPI_Status status = NVAPI_OK;
NvU32 deviceCount = 0;
NV_DISPLAYCONFIG_PATH_INFO_V2 *  pathInfo = NULL;

status = NvAPI_Initialize();
if (status == NVAPI_OK) {
    status = NvAPI_DISP_GetDisplayConfig(&deviceCount, pathInfo);
    if ((status == NVAPI_OK) && (deviceCount > 0)) {
        pathInfo = new NV_DISPLAYCONFIG_PATH_INFO_V2[deviceCount];
        for (int i = 0; i < deviceCount; i++)
        {
            pathInfo[i].targetInfo = 0;
            pathInfo[i].targetInfoCount = 0;
            pathInfo[i].version = NV_DISPLAYCONFIG_PATH_INFO_VER2;
            pathInfo[i].sourceModeInfo = 0;
            pathInfo[i].reserved = 0;
        }

        status = NvAPI_DISP_GetDisplayConfig(&deviceCount, pathInfo);

        if (status == NVAPI_OK) {
            for (int i = 0; i < deviceCount; i++)
            {
                pathInfo[i].sourceModeInfo = new NV_DISPLAYCONFIG_SOURCE_MODE_INFO_V1;
                pathInfo[i].sourceModeInfo->reserved = 0;
                pathInfo[i].targetInfo = new NV_DISPLAYCONFIG_PATH_TARGET_INFO_V2[pathInfo[i].targetInfoCount];
                for (int j = 0; j < pathInfo[i].targetInfoCount; j++) {
                    pathInfo[i].targetInfo[j].details = 0;
                }
            }
        }

        status = NvAPI_DISP_GetDisplayConfig(&deviceCount, pathInfo);

        for (int i = 0; i < deviceCount; i++)
        {
            if (pathInfo[i].sourceModeInfo) delete pathInfo[i].sourceModeInfo;
            if (pathInfo[i].targetInfo) delete [] pathInfo[i].targetInfo;
        }
        delete[] pathInfo;
    }
}

关于c++ - 如何使用NvAPI_DISP_GetDisplayConfig?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10737051/

10-11 19:03