1

使用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);

在我的系统上,第一次通话后 count = 2。NvAPI_DISP_GetDisplayConfig 上的注释说:

NVAPI_INTERFACE NvAPI_DISP_GetDisplayConfig(
__inout NvU32 * pathInfoCount,
__out_ecount_full_opt *pathInfoCount NV_DISPLAYCONFIG_PATH_INFO * pathInfo
)

描述:此 API 允许调用者检索当前的全局显示
配置。
用法:调用者可能必须调用此函数 3 次以获取所有必需的
配置详细信息,如下所示:
第一遍:调用者应调用 NvAPI_DISP_GetDisplayConfig() 并将 pathInfo 设置
为 NULL 以获取 pathInfoCount。 第二遍:根据要获取的 pathInfoCount(from First Pass)
的数量为 pathInfo 分配内存//!
//! 目标信息计数。如果
需要 sourceModeInfo 分配内存,或者可以将其初始化为 NULL。
第三遍(可选,仅在需要目标信息时才需要):
根据数量为 targetInfo 分配内存
targetInfoCount(来自第二遍)。支持的操作系统:Windows Vista 及更高版本

谢谢。

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

4

3 回答 3

2

这应该适合你:

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;
    }
}
于 2012-08-17T20:12:58.453 回答
0

马科斯的回答更彻底。我相信您的代码中的具体问题是您没有初始化第二个 pathInfo。所以你需要添加:

configinfos[1].version = NV_DISPLAYCONFIG_PATH_INFO_VER;

总是初始化你的记忆也是一种很好的形式。因此,分配后您应该立即执行以下操作:

memset(configinfos, 0, sizeof(NV_DISPLAYCONFIG_PATH_INFO) * count);

或者您可以单独设置这些值。

于 2012-10-02T19:03:17.023 回答
-1

NvAPI 附带的示例代码中有一个名为 DisplayConfiguration 的示例,它完全利用了 GetDisplayConfig。从 DisplayConfiguration.cpp 复制粘贴函数:

NvAPI_Status AllocateAndGetDisplayConfig(NvU32* pathInfoCount, NV_DISPLAYCONFIG_PATH_INFO** pPathInfo)
{
    NvAPI_Status ret;

    // Retrieve the display path information
    NvU32 pathCount                         = 0;
    NV_DISPLAYCONFIG_PATH_INFO *pathInfo    = NULL;

    ret = NvAPI_DISP_GetDisplayConfig(&pathCount, NULL);
    if (ret != NVAPI_OK)    return ret;

    pathInfo = (NV_DISPLAYCONFIG_PATH_INFO*) malloc(pathCount * sizeof(NV_DISPLAYCONFIG_PATH_INFO));
    if (!pathInfo)
    {
        return NVAPI_OUT_OF_MEMORY;
    }

    memset(pathInfo, 0, pathCount * sizeof(NV_DISPLAYCONFIG_PATH_INFO));
    for (NvU32 i = 0; i < pathCount; i++)
    {
        pathInfo[i].version = NV_DISPLAYCONFIG_PATH_INFO_VER;
    }

    // Retrieve the targetInfo counts
    ret = NvAPI_DISP_GetDisplayConfig(&pathCount, pathInfo);
    if (ret != NVAPI_OK)
    {
        return ret;
    }

    for (NvU32 i = 0; i < pathCount; i++)
    {
        // Allocate the source mode info
        pathInfo[i].sourceModeInfo = (NV_DISPLAYCONFIG_SOURCE_MODE_INFO*) malloc(sizeof(NV_DISPLAYCONFIG_SOURCE_MODE_INFO));
        if (pathInfo[i].sourceModeInfo == NULL)
        {
            return NVAPI_OUT_OF_MEMORY;
        }
        memset(pathInfo[i].sourceModeInfo, 0, sizeof(NV_DISPLAYCONFIG_SOURCE_MODE_INFO));

        // Allocate the target array
        pathInfo[i].targetInfo = (NV_DISPLAYCONFIG_PATH_TARGET_INFO*) malloc(pathInfo[i].targetInfoCount * sizeof(NV_DISPLAYCONFIG_PATH_TARGET_INFO));
        if (pathInfo[i].targetInfo == NULL)
        {
            return NVAPI_OUT_OF_MEMORY;
        }
        // Allocate the target details
        memset(pathInfo[i].targetInfo, 0, pathInfo[i].targetInfoCount * sizeof(NV_DISPLAYCONFIG_PATH_TARGET_INFO));
        for (NvU32 j = 0 ; j < pathInfo[i].targetInfoCount ; j++)
        {
            pathInfo[i].targetInfo[j].details = (NV_DISPLAYCONFIG_PATH_ADVANCED_TARGET_INFO*) malloc(sizeof(NV_DISPLAYCONFIG_PATH_ADVANCED_TARGET_INFO));    
            memset(pathInfo[i].targetInfo[j].details, 0, sizeof(NV_DISPLAYCONFIG_PATH_ADVANCED_TARGET_INFO));
            pathInfo[i].targetInfo[j].details->version = NV_DISPLAYCONFIG_PATH_ADVANCED_TARGET_INFO_VER;
        }
    }

    // Retrieve the full path info
    ret = NvAPI_DISP_GetDisplayConfig(&pathCount, pathInfo);
    if (ret != NVAPI_OK)    
    {
        return ret;
    }

    *pathInfoCount = pathCount;
    *pPathInfo = pathInfo;
    return NVAPI_OK;
}

现在这个函数可以很容易地使用:

NV_DISPLAYCONFIG_PATH_INFO *pathInfo = NULL;
NvU32 pathCount = 0;
_allocateAndGetDisplayConfig(&pathCount, &pathInfo);
// Do whatever you need with the queried display data here
于 2014-08-14T07:22:18.083 回答