本文主要是介绍读取Nvidia驱动版本号,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Solution 1
读取注册表信息,不可取。很多电脑的注册表信息丢失不全。
Solution 2
通过dxgi读取显卡版本号。在台式机上正常,双显卡的笔记本电脑端,由于默认的显卡为核显,所以读取的为核显版本号。
Solution 3
通过调用命令行管道,执行Nvidia Driver目录下的exe,截取返回信息中的版本号
#include <string>
#include "Windows.h"#include <QDebug>
字符转换
//将string转换成wstring
std::wstring string2wstring(std::string str)
{std::wstring result;//获取缓冲区大小,并申请空间,缓冲区大小按字符计算int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);TCHAR* buffer = new TCHAR[len + 1];//多字节编码转换成宽字节编码MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);buffer[len] = '\0'; //添加字符串结尾//删除缓冲区并返回值result.append(buffer);delete[] buffer;return result;
}//将wstring转换成string
std::string wstring2string(std::wstring wstr)
{std::string result;//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);char* buffer = new char[len + 1];//宽字节编码转换成多字节编码WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);buffer[len] = '\0';//删除缓冲区并返回值result.append(buffer);delete[] buffer;return result;
}
管道命令
std::wstring ExeCmd(std::wstring pszCmd)
{// 创建匿名管道SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};HANDLE hRead, hWrite;if (!CreatePipe(&hRead, &hWrite, &sa, 0)){return TEXT(" ");}// 设置命令行进程启动信息(以隐藏方式启动命令并定位其输出到hWriteSTARTUPINFO si = {sizeof(STARTUPINFO)};GetStartupInfo(&si);si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;si.wShowWindow = SW_HIDE;si.hStdError = hWrite;si.hStdOutput = hWrite;// 启动命令行PROCESS_INFORMATION pi;if (!CreateProcess(NULL, (LPWSTR)pszCmd.c_str(), NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi)){return TEXT("Cannot create process");}// 立即关闭hWriteCloseHandle(hWrite);// 读取命令行返回值std::string strRetTmp;char buff[1024] = {0};DWORD dwRead = 0;strRetTmp = buff;while (ReadFile(hRead, buff, 1024, &dwRead, NULL)){strRetTmp += buff;}CloseHandle(hRead);LPCSTR pszSrc = strRetTmp.c_str();int nLen = MultiByteToWideChar(CP_ACP, 0, buff, -1, NULL, 0);if (nLen == 0)return std::wstring(L"");wchar_t* pwszDst = new wchar_t[nLen];if (!pwszDst)return std::wstring(L"");MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, pwszDst, nLen);std::wstring strRet(pwszDst);delete[] pwszDst;pwszDst = NULL;return strRet;
}
测试部分
std::wstring cmdstr = L"C:/Program Files/NVIDIA Corporation/NVSMI/nvidia-smi.exe";std::wstring resultwstr = ExeCmd(cmdstr);std::string resultstr = wstring2string(resultwstr);std::string driverVersionInfo = "Driver Version:";int VersionStartLocation = resultstr.find(driverVersionInfo) + driverVersionInfo.size() + 1;std::string versionNumber = resultstr.substr(VersionStartLocation,6);qDebug() << QString::fromStdString(versionNumber);
正常来说显卡驱动都安装在默认位置。。。
这篇关于读取Nvidia驱动版本号的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!