nVidia driver version from WMI is not what I want

2019-05-12 22:19发布

I want to get driver version of nVidia video card. So I used WMI and get data from "DriverVersion" obejct of "Win32_VideoController" class. But it was like "9.18.13.1106"(file version) and what I wanted is something like "311.06"(treiber version). Where can I get that information? If it is impossible on WMI, I want to know other way to get that. Thanks.

标签: c++ wmi nvidia
1条回答
爷、活的狠高调
2楼-- · 2019-05-12 22:58

You can do this using NVML from nVidia's Tesla Deployment Kit. You can retrieve the internal driver version (the one you're accustomed to seeing for an nVidia driver) with code like this:

#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();
}

Note that if you're writing this purely for your own use on a machine where you're free to edit the PATH, you can simplify this quite a bit. Most of the code deals with the fact that this uses NVML.DLL, which is in a directory that's not normally on the path, so the code loads that dynamically, and uses GetProcAddress to find the functions in it that we need to use. In this case, we're only using three functions, so it's not all that difficult to deal with, but it still at drastically increases the length of the code.

If we could ignore all that nonsense, the real code would just come out to something on this general order:

nvmlInit();
nvmlSystemGetDriverVersion(result, sizeof(result));
std::cout << result;
nvmlShutdown();

Anyway, to build it, you'll need a command line something like:

 cl -Ic:\tdk\nvml\include nv_driver_version.cpp

...assuming you've installed the Tesla Deployment Kit at c:\tdk.

In any case, yes, I've tested this to at least some degree. On my desktop it prints out:

nVidia Driver version: 314.22

...which matches what I have installed.

查看更多
登录 后发表回答