C++獲取GPU顯卡信息的示例代碼
更新時間:2024年02月04日 09:49:19 作者:草上爬
這篇文章主要為大家詳細介紹了如何使用C++獲取GPU顯卡信息,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下
C++獲取GPU顯卡信息
#include <Windows.h> #include <iostream> #include <DXGI.h> #include <vector> using namespace std; std::string WStringToString(const std::wstring &wstr) { std::string str(wstr.length(), ' '); std::copy(wstr.begin(), wstr.end(), str.begin()); return str; } int main() { // 參數(shù)定義 IDXGIFactory * pFactory; IDXGIAdapter * pAdapter; std::vector <IDXGIAdapter*> vAdapters; // 顯卡 // 顯卡的數(shù)量 int iAdapterNum = 0; // 創(chuàng)建一個DXGI工廠 HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&pFactory)); if (FAILED(hr)) return -1; // 枚舉適配器 while (pFactory->EnumAdapters(iAdapterNum, &pAdapter) != DXGI_ERROR_NOT_FOUND) { vAdapters.push_back(pAdapter); ++iAdapterNum; } // 信息輸出 cout << "===============獲取到" << iAdapterNum << "塊顯卡===============" << endl; for (size_t i = 0; i < vAdapters.size(); i++) { // 獲取信息 DXGI_ADAPTER_DESC adapterDesc; vAdapters[i]->GetDesc(&adapterDesc); wstring aa(adapterDesc.Description); std::string bb = WStringToString(aa); // 輸出顯卡信息 cout << "系統(tǒng)視頻內(nèi)存:" << adapterDesc.DedicatedSystemMemory / 1024 / 1024 << "M" << endl; cout << "專用視頻內(nèi)存:" << adapterDesc.DedicatedVideoMemory / 1024 / 1024 << "M" << endl; cout << "共享系統(tǒng)內(nèi)存:" << adapterDesc.SharedSystemMemory / 1024 / 1024 << "M" << endl; cout << "設備描述:" << bb.c_str()<< endl; cout << "設備ID:" << adapterDesc.DeviceId << endl; cout << "PCI ID修正版本:" << adapterDesc.Revision << endl; cout << "子系統(tǒng)PIC ID:" << adapterDesc.SubSysId << endl; cout << "廠商編號:" << adapterDesc.VendorId << endl; // 輸出設備 IDXGIOutput * pOutput; std::vector<IDXGIOutput*> vOutputs; // 輸出設備數(shù)量 int iOutputNum = 0; while (vAdapters[i]->EnumOutputs(iOutputNum, &pOutput) != DXGI_ERROR_NOT_FOUND) { vOutputs.push_back(pOutput); iOutputNum++; } cout << "-----------------------------------------" << endl; cout << "獲取到" << iOutputNum << "個顯示設備:" << endl; cout << endl; for (size_t n = 0; n < vOutputs.size(); n++) { // 獲取顯示設備信息 DXGI_OUTPUT_DESC outputDesc; vOutputs[n]->GetDesc(&outputDesc); // 獲取設備支持 UINT uModeNum = 0; DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM; UINT flags = DXGI_ENUM_MODES_INTERLACED; vOutputs[n]->GetDisplayModeList(format, flags, &uModeNum, 0); DXGI_MODE_DESC * pModeDescs = new DXGI_MODE_DESC[uModeNum]; vOutputs[n]->GetDisplayModeList(format, flags, &uModeNum, pModeDescs); cout << "顯示設備名稱:" << outputDesc.DeviceName << endl; cout << "顯示設備當前分辨率:" << outputDesc.DesktopCoordinates.right - outputDesc.DesktopCoordinates.left << "*" << outputDesc.DesktopCoordinates.bottom - outputDesc.DesktopCoordinates.top << endl; cout << endl; // 所支持的分辨率信息 cout << "分辨率信息:" << endl; for (UINT m = 0; m < uModeNum; m++) { cout << "== 分辨率:" << pModeDescs[m].Width << "*" << pModeDescs[m].Height << " 刷新率" << (pModeDescs[m].RefreshRate.Numerator) / (pModeDescs[m].RefreshRate.Denominator) << endl; } } vOutputs.clear(); } vAdapters.clear(); system("pause"); return 0; }
在鏈接器中加入DXGI.lib
如果沒安裝顯驅(qū)動,無法獲取GPU信息。
知識補充
C++獲取設備顯示信息
/************************************************************************ FileName:main.cpp Descript:獲取顯卡信息 Author:絕望的老貓 Date:2012/12/23 ************************************************************************/ #include <Windows.h> #include <iostream> #include <DXGI.h> #include <vector> using namespace std; int main() { // 參數(shù)定義 IDXGIFactory * pFactory; IDXGIAdapter * pAdapter; std::vector <IDXGIAdapter*> vAdapters; // 顯卡 // 顯卡的數(shù)量 int iAdapterNum = 0; // 創(chuàng)建一個DXGI工廠 HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&pFactory) ); if (FAILED(hr)) return -1; // 枚舉適配器 while(pFactory->EnumAdapters(iAdapterNum, &pAdapter) != DXGI_ERROR_NOT_FOUND) { vAdapters.push_back(pAdapter); ++iAdapterNum; } // 信息輸出 cout<<"===============獲取到"<<iAdapterNum<<"塊顯卡==============="<<endl; for (size_t i=0;i<vAdapters.size(); i++) { // 獲取信息 DXGI_ADAPTER_DESC adapterDesc; vAdapters[i]->GetDesc(&adapterDesc); // 輸出顯卡信息 cout<<"系統(tǒng)視頻內(nèi)存:"<<adapterDesc.DedicatedSystemMemory/1024/1024<<"M"<<endl; cout<<"專用視頻內(nèi)存:"<<adapterDesc.DedicatedVideoMemory/1024/1024<<"M"<<endl; cout<<"共享系統(tǒng)內(nèi)存:"<<adapterDesc.SharedSystemMemory/1024/1024<<"M"<<endl; cout<<"設備描述:"<<adapterDesc.Description<<endl; cout<<"設備ID:"<<adapterDesc.DeviceId<<endl; cout<<"PCI ID修正版本:"<<adapterDesc.Revision<<endl; cout<<"子系統(tǒng)PIC ID:"<<adapterDesc.SubSysId<<endl; cout<<"廠商編號:"<<adapterDesc.VendorId<<endl; // 輸出設備 IDXGIOutput * pOutput; std::vector<IDXGIOutput*> vOutputs; // 輸出設備數(shù)量 int iOutputNum = 0; while(vAdapters[i]->EnumOutputs(iOutputNum,&pOutput)!= DXGI_ERROR_NOT_FOUND) { vOutputs.push_back(pOutput); iOutputNum++; } cout<<"-----------------------------------------"<<endl; cout<<"獲取到"<<iOutputNum<<"個顯示設備:"<<endl; cout<<endl; for (size_t n=0;n<vOutputs.size();n++) { // 獲取顯示設備信息 DXGI_OUTPUT_DESC outputDesc; vOutputs[n]->GetDesc(&outputDesc); // 獲取設備支持 UINT uModeNum = 0; DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM; UINT flags = DXGI_ENUM_MODES_INTERLACED; vOutputs[n]->GetDisplayModeList( format, flags, &uModeNum, 0); DXGI_MODE_DESC * pModeDescs = new DXGI_MODE_DESC[uModeNum]; vOutputs[n]->GetDisplayModeList( format, flags, &uModeNum, pModeDescs); cout<<"顯示設備名稱:"<<outputDesc.DeviceName<<endl; cout<<"顯示設備當前分辨率:"<<outputDesc.DesktopCoordinates.right-outputDesc.DesktopCoordinates.left <<"*"<<outputDesc.DesktopCoordinates.bottom-outputDesc.DesktopCoordinates.top<<endl; cout<<endl; // 所支持的分辨率信息 cout<<"分辨率信息:"<<endl; for (UINT m=0;m<uModeNum;m++) { cout<<"== 分辨率:"<<pModeDescs[m].Width<<"*"<<pModeDescs[m].Height<<" 刷新率"<<(pModeDescs[m].RefreshRate.Numerator)/(pModeDescs[m].RefreshRate.Denominator)<<endl; } } vOutputs.clear(); } vAdapters.clear(); system("pause"); return 0; }
到此這篇關于C++獲取GPU顯卡信息的示例代碼的文章就介紹到這了,更多相關C++獲取GPU顯卡信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
VSCode下.json文件的編寫之(1) linux/g++ (2).json中參數(shù)與預定義變量的意義解釋
這篇文章主要介紹了VSCode下.json文件的編寫之(1) linux/g++ (2).json中參數(shù)與預定義變量的意義解釋,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03關于C++STL string類的介紹及模擬實現(xiàn)
這篇文章主要介紹了關于C++STL string類的介紹及模擬實現(xiàn)的相關資料,需要的朋友可以參考下面具體的文章內(nèi)容2021-09-09