WMI获取的nVidia驱动程序版本不符合要求。

3

我想获取nVidia视频卡的驱动版本。 所以我使用WMI并从“Win32_VideoController”类的“DriverVersion”对象中获取数据。 但它显示的是文件版本,如“9.18.13.1106”,而我想要的是类似于“311.06”的驱动程序版本。 在哪里可以获取该信息? 如果无法通过WMI获取,我想知道其他获取该信息的方法。 谢谢。


注册表是否显示您想要的内容(没有NVIDIA驱动程序可供检查)? - Austin T French
2个回答

2
您可以使用nVidia的Tesla Deployment Kit中的NVML来完成此操作。您可以使用以下代码检索内部驱动程序版本(您习惯于在nVidia驱动程序中看到的版本号):
#include <iostream>
#include <string>
#include <stdlib.h>
#include <nvml.h>
#include <windows.h>

namespace { 
typedef nvmlReturn_t (*init)();
typedef nvmlReturn_t (*shutdown)();
typedef nvmlReturn_t (*get_version)(char *, unsigned);

class NVML {
    init nvmlInit;
    shutdown nvmlShutdown;
    get_version nvmlGetDriverVersion;

    std::string find_dll() {
        std::string loc(getenv("ProgramW6432"));
        loc += "\\Nvidia Corporation\\nvsmi\\nvml.dll";
        return loc;
    }

public:    
    NVML() {
        HMODULE lib = LoadLibrary(find_dll().c_str());
        nvmlInit = (init)GetProcAddress(lib, "nvmlInit");
        nvmlShutdown = (shutdown)GetProcAddress(lib, "nvmlShutdown");
        nvmlGetDriverVersion = (get_version)GetProcAddress(lib, "nvmlSystemGetDriverVersion");

        if (NVML_SUCCESS != nvmlInit())
            throw(std::runtime_error("Unable to initialize NVML"));
    }

    std::string get_ver() {
        char buffer[81];
        nvmlGetDriverVersion(buffer, sizeof(buffer));
        return std::string(buffer);
    }

    ~NVML() {
        if (NVML_SUCCESS != nvmlShutdown())
            throw(std::runtime_error("Unable to shut down NVML"));
    }
};
}

int main() {  
    std::cout << "nVidia Driver version: " << NVML().get_ver();
}

请注意,如果您只是为自己在一台可以编辑PATH的机器上编写代码,那么您可以大大简化它。该代码大部分涉及到使用不常见的路径中的NVML.DLL,因此该代码动态加载该DLL,并使用GetProcAddress查找我们需要使用的其中三个函数。在这种情况下,我们仅使用三个函数,因此处理起来并不困难,但它仍然大大增加了代码的长度。
如果我们可以忽略所有这些无聊细节,真正的代码实际上会非常简单:
nvmlInit();
nvmlSystemGetDriverVersion(result, sizeof(result));
std::cout << result;
nvmlShutdown();

无论如何,要构建它,您需要类似于以下命令行:
 cl -Ic:\tdk\nvml\include nv_driver_version.cpp

假设您已经在 c:\tdk 安装了特斯拉部署工具包。
无论如何,是的,我已经进行了一些测试。在我的桌面上,它会打印出:
nVidia Driver version: 314.22

"...这与我安装的相匹配。"

1
我非常感激你 :) - mayTree
这对于32位应用程序(Windows)不起作用。 - Sergio Basurco

1

如何在Win64平台上通过C++获取Nvidia驱动程序版本:

下载NVAPI https://developer.nvidia.com/rtx/path-tracing/nvapi/get-started,大小约为几MB。

下载的压缩包主文件夹中包含多个头文件,其中之一是nvapi.h。这些头文件需要用于编译。amd64子文件夹包含nvapi64.lib,需要用于链接。以下代码将显示驱动程序版本:

#include <iostream>

extern "C" {
    #include "nvapi.h"
}

int main() {
    NvAPI_Status status = NVAPI_OK;
    NvAPI_ShortString str;
    
    status = NvAPI_Initialize();
    if (status == NVAPI_LIBRARY_NOT_FOUND) {
        //in this case NvAPI_GetErrorMessage() will only return an empty string
        std::printf("error no nvidia driver found\n");

    } else if (status != NVAPI_OK) {
        NvAPI_GetErrorMessage(status, str);
        std::printf("error initializing nvapi: %s\n", str);
    }

    NvU32 version = 0;
    NvAPI_ShortString branch;
    status = NvAPI_SYS_GetDriverAndBranchVersion(&version, branch);
    if (status != NVAPI_OK) {
        NvAPI_GetErrorMessage(status, str);
        std::printf("error getting driver version: %s\n", str);

    } else {
        std::printf("driver version %d.%d", version / 100, version % 100);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接