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)建一個(gè)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 << "設(shè)備描述:" << bb.c_str()<< endl; cout << "設(shè)備ID:" << adapterDesc.DeviceId << endl; cout << "PCI ID修正版本:" << adapterDesc.Revision << endl; cout << "子系統(tǒng)PIC ID:" << adapterDesc.SubSysId << endl; cout << "廠商編號(hào):" << adapterDesc.VendorId << endl; // 輸出設(shè)備 IDXGIOutput * pOutput; std::vector<IDXGIOutput*> vOutputs; // 輸出設(shè)備數(shù)量 int iOutputNum = 0; while (vAdapters[i]->EnumOutputs(iOutputNum, &pOutput) != DXGI_ERROR_NOT_FOUND) { vOutputs.push_back(pOutput); iOutputNum++; } cout << "-----------------------------------------" << endl; cout << "獲取到" << iOutputNum << "個(gè)顯示設(shè)備:" << endl; cout << endl; for (size_t n = 0; n < vOutputs.size(); n++) { // 獲取顯示設(shè)備信息 DXGI_OUTPUT_DESC outputDesc; vOutputs[n]->GetDesc(&outputDesc); // 獲取設(shè)備支持 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 << "顯示設(shè)備名稱:" << outputDesc.DeviceName << endl; cout << "顯示設(shè)備當(dāng)前分辨率:" << 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
如果沒(méi)安裝顯驅(qū)動(dòng),無(wú)法獲取GPU信息。
知識(shí)補(bǔ)充
C++獲取設(shè)備顯示信息
/************************************************************************ 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)建一個(gè)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<<"設(shè)備描述:"<<adapterDesc.Description<<endl; cout<<"設(shè)備ID:"<<adapterDesc.DeviceId<<endl; cout<<"PCI ID修正版本:"<<adapterDesc.Revision<<endl; cout<<"子系統(tǒng)PIC ID:"<<adapterDesc.SubSysId<<endl; cout<<"廠商編號(hào):"<<adapterDesc.VendorId<<endl; // 輸出設(shè)備 IDXGIOutput * pOutput; std::vector<IDXGIOutput*> vOutputs; // 輸出設(shè)備數(shù)量 int iOutputNum = 0; while(vAdapters[i]->EnumOutputs(iOutputNum,&pOutput)!= DXGI_ERROR_NOT_FOUND) { vOutputs.push_back(pOutput); iOutputNum++; } cout<<"-----------------------------------------"<<endl; cout<<"獲取到"<<iOutputNum<<"個(gè)顯示設(shè)備:"<<endl; cout<<endl; for (size_t n=0;n<vOutputs.size();n++) { // 獲取顯示設(shè)備信息 DXGI_OUTPUT_DESC outputDesc; vOutputs[n]->GetDesc(&outputDesc); // 獲取設(shè)備支持 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<<"顯示設(shè)備名稱:"<<outputDesc.DeviceName<<endl; cout<<"顯示設(shè)備當(dāng)前分辨率:"<<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; }
到此這篇關(guān)于C++獲取GPU顯卡信息的示例代碼的文章就介紹到這了,更多相關(guān)C++獲取GPU顯卡信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談2路插入排序算法及其簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了淺談2路插入排序算法及其簡(jiǎn)單實(shí)現(xiàn),雖算不上是常用的排序方法,但在數(shù)據(jù)庫(kù)等方面依然有用上的機(jī)會(huì),需要的朋友可以參考下2015-08-08VSCode下.json文件的編寫之(1) linux/g++ (2).json中參數(shù)與預(yù)定義變量的意義解釋
這篇文章主要介紹了VSCode下.json文件的編寫之(1) linux/g++ (2).json中參數(shù)與預(yù)定義變量的意義解釋,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單班級(jí)成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單班級(jí)成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03c語(yǔ)言實(shí)現(xiàn)兩個(gè)單鏈表的交叉合并方式
今天小編就為大家分享一篇c語(yǔ)言實(shí)現(xiàn)兩個(gè)單鏈表的交叉合并方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12關(guān)于C++STL string類的介紹及模擬實(shí)現(xiàn)
這篇文章主要介紹了關(guān)于C++STL string類的介紹及模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下面具體的文章內(nèi)容2021-09-09C++對(duì)象的動(dòng)態(tài)建立與釋放詳解
我們知道可以用new運(yùn)算符可以動(dòng)態(tài)的分配內(nèi)存,用delete運(yùn)算符可以釋放這些內(nèi)存。當(dāng)我們使用new運(yùn)算符動(dòng)態(tài)的分配一個(gè)內(nèi)存之后,會(huì)自動(dòng)返回一個(gè)該內(nèi)存段的起始地址,也就是指針。2013-10-10