English 中文(简体)
如何使用 NvAPI_ DISP_ Get DisplayConfig?
原标题:How to use NvAPI_DISP_GetDisplayConfig?
  • 时间:2012-05-24 11:57:27
  •  标签:
  • c++
  • nvapi

When using the NVAPI I m having problems with NvAPI_DISP_GetDisplayConfig. I get an AppCrash on the second call to NvAPI_DISP_GetDisplayConfig. Can t seem to figure out why.

NvU32 count = 0;
status = NvAPI_DISP_GetDisplayConfig(&count, NULL);
if (status != NVAPI_OK) 
    PrintError(status);
printf("Configs: %i
", 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);

On my system, count = 2 after the first call. The notes on NvAPI_DISP_GetDisplayConfig say:

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

DESCRIPTION: This API lets caller retrieve the current global display
configuration.
USAGE: The caller might have to call this three times to fetch all the required
configuration details as follows:
First Pass: Caller should Call NvAPI_DISP_GetDisplayConfig() with pathInfo set
to NULL to fetch pathInfoCount.
Second Pass: Allocate memory for pathInfo with respect to the number of
pathInfoCount(from First Pass) to fetch //! //! targetInfoCount. If
sourceModeInfo is needed allocate memory or it can be initialized to NULL.
Third Pass(Optional, only required if target information is required): Allocate
memory for targetInfo with respect to number of
targetInfoCount(from Second Pass). SUPPORTED OS: Windows Vista and higher

谢谢

编辑 : 我还试图设置 configinfos [0]. sourceModeInfo = NULLL = 无效。 我还尝试过在数组中复制设置全部.version 和.poterModeInfo 。

最佳回答

这应该对你有用:

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;
    }
}
问题回答

Marcos 的回答更全面。 我认为您的代码中的具体问题是 您没有初始化第二个路径Info 。 所以您需要添加 :

configinfos[1].version = NV_DISPLAYCONFIG_PATH_INFO_VER;

总是不记内存也是好形式。所以在分配后,你应该立即做到:

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

或者您可以单独设定值。

NvAPI 在样本代码中有一个名为 Doward Configination 的样本, 与 NvAPI 相配, 完全利用 Get Display Config 。 复制从 Doward Configation. 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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签