參考文檔:http://blog.csdn.net/caoshangpa/article/details/78623044
http://blog.csdn.net/tody_guo/article/details/26365573
#include <DXGI.h>
方法一:
/****.h 獲取顯卡信息*****/
UFUNCTION(BlueprintCallable, Category = "DCFUtil")
static void GetDisplayCardInfo();
void UDCFBlueprintFunctionLibrary::GetDisplayCardInfo()
{
//ASSERT(pDXGIFactory != 0);
HINSTANCE hDXGI = LoadLibrary(L"dxgi.dll");
typedef HRESULT(WINAPI* LPCREATEDXGIFACTORY)(REFIID, void**);
LPCREATEDXGIFACTORY pCreateDXGIFactory = nullptr;
IDXGIFactory* pDXGIFactory = nullptr;
// We prefer the use of DXGI 1.1
pCreateDXGIFactory = (LPCREATEDXGIFACTORY)GetProcAddress(hDXGI, "CreateDXGIFactory1");
if (!pCreateDXGIFactory)
{
pCreateDXGIFactory = (LPCREATEDXGIFACTORY)GetProcAddress(hDXGI, "CreateDXGIFactory");
if (!pCreateDXGIFactory)
{
FreeLibrary(hDXGI);
//wprintf(L"ERROR: dxgi.dll missing entry-point\n");
UE_LOG(LogClass, Log, TEXT("ERROR: dxgi.dll missing entry-point\n"));
return ;
}
}
HRESULT hr = pCreateDXGIFactory(__uuidof(IDXGIFactory), (LPVOID*)&pDXGIFactory);
if (!pDXGIFactory)
return;
for (UINT index = 0; ; ++index)
{
IDXGIAdapter* pAdapter = nullptr;
HRESULT hr = pDXGIFactory->EnumAdapters(index, &pAdapter);
if (FAILED(hr)) // DXGIERR_NOT_FOUND is expected when the end of the list is hit
break;
DXGI_ADAPTER_DESC desc;
memset(&desc, 0, sizeof(DXGI_ADAPTER_DESC));
if (SUCCEEDED(pAdapter->GetDesc(&desc)))
{
//顯卡名稱
UE_LOG(LogClass, Log, TEXT("Adapter: [%u] %s\n"), index, desc.Description);
for (UINT iOutput = 0; ; ++iOutput)
{
IDXGIOutput* pOutput = nullptr;
hr = pAdapter->EnumOutputs(iOutput, &pOutput);
if (FAILED(hr)) // DXGIERR_NOT_FOUND is expected when the end of the list is hit
break;
DXGI_OUTPUT_DESC outputDesc;
memset(&outputDesc, 0, sizeof(DXGI_OUTPUT_DESC));
if (SUCCEEDED(pOutput->GetDesc(&outputDesc)))
{
UE_LOG(LogClass, Log, TEXT("hMonitor: 0x%0.8Ix\n"), (DWORD_PTR)outputDesc.Monitor);
UE_LOG(LogClass, Log, TEXT("hMonitor Device Name: %s\n"), outputDesc.DeviceName);
}
pOutput->Release();
}
//顯示內存
UE_LOG(LogClass,Log,TEXT("Dedicated Video Memory: %Iu MB\n" "Dedicated System Memory: %Iu MB\n" "Shared System Memory: %Iu MB\n"), desc.DedicatedVideoMemory / 1024 / 1024, desc.DedicatedSystemMemory / 1024 / 1024, desc.SharedSystemMemory / 1024 / 1024);
}
pAdapter->Release();
}
}
方法二:
//.cpp
static string WStringToString(const wstring &wstr);
/****獲取顯卡信息*****/
UFUNCTION(BlueprintCallable, Category = "DCFUtil")
static int GetDisplayCardInfo2();
std::string UDCFBlueprintFunctionLibrary::WStringToString(const std::wstring & wstr)
{
std::string str(wstr.length(), ' ');
std::copy(wstr.begin(), wstr.end(), str.begin());
return str;
}
int UDCFBlueprintFunctionLibrary::GetDisplayCardInfo2()
{
// 參數定義
typedef HRESULT(WINAPI* LPCREATEDXGIFACTORY)(REFIID, void**);
LPCREATEDXGIFACTORY pCreateDXGIFactory = nullptr;
IDXGIFactory* pDXGIFactory = nullptr;
IDXGIAdapter * pAdapter = nullptr;
TArray<IDXGIAdapter*> vAdapters; // 顯卡
// 顯卡的數量
int iAdapterNum = 0;
HINSTANCE hDXGI = LoadLibrary(L"dxgi.dll");
// 創建一個DXGI工廠
pCreateDXGIFactory = (LPCREATEDXGIFACTORY)GetProcAddress(hDXGI, "CreateDXGIFactory1");
if (!pCreateDXGIFactory)
{
pCreateDXGIFactory = (LPCREATEDXGIFACTORY)GetProcAddress(hDXGI, "CreateDXGIFactory");
if (!pCreateDXGIFactory)
{
FreeLibrary(hDXGI);
UE_LOG(LogClass, Log, TEXT("ERROR: dxgi.dll missing entry-point\n"));
return -1;
}
}
HRESULT hr = pCreateDXGIFactory(__uuidof(IDXGIFactory), (LPVOID*)&pDXGIFactory);
if (FAILED(hr))
return -1;
// 枚舉適配器
while (pDXGIFactory->EnumAdapters(iAdapterNum, &pAdapter) != DXGI_ERROR_NOT_FOUND)
{
vAdapters.Push(pAdapter);
++iAdapterNum;
}
// 信息輸出
UE_LOG(LogClass, Log, TEXT("===============獲取到 %d 塊顯卡===============\n"), iAdapterNum);
for (size_t i = 0; i < vAdapters.Num(); i++)
{
// 獲取信息
DXGI_ADAPTER_DESC adapterDesc;
vAdapters[i]->GetDesc(&adapterDesc);
wstring aa(adapterDesc.Description);
std::string bb = WStringToString(aa);
FString fbb = bb.c_str();
// 輸出顯卡信息
UE_LOG(LogClass, Log, TEXT("系統視頻內存: %d M\n"), adapterDesc.DedicatedSystemMemory / 1024 / 1024);
UE_LOG(LogClass, Log, TEXT("專用視頻內存: %d M\n"), adapterDesc.DedicatedVideoMemory / 1024 / 1024);
UE_LOG(LogClass, Log, TEXT("共享系統內存: %d M\n"), adapterDesc.SharedSystemMemory / 1024 / 1024);
UE_LOG(LogClass, Log, TEXT("設備描述: %s\n"), *fbb);
UE_LOG(LogClass, Log, TEXT("設備ID: %d\n"), adapterDesc.DeviceId);
UE_LOG(LogClass, Log, TEXT("PCI ID修正版本: %d\n"), adapterDesc.Revision);
UE_LOG(LogClass, Log, TEXT("子系統PIC ID: %d\n"), adapterDesc.SubSysId);
UE_LOG(LogClass, Log, TEXT("廠商編號: %d\n"), adapterDesc.VendorId);
// 輸出設備
IDXGIOutput * pOutput;
TArray<IDXGIOutput*> vOutputs;
// 輸出設備數量
int iOutputNum = 0;
while (vAdapters[i]->EnumOutputs(iOutputNum, &pOutput) != DXGI_ERROR_NOT_FOUND)
{
vOutputs.Push(pOutput);
iOutputNum++;
}
UE_LOG(LogClass, Log, TEXT("-----------------------------------------"));
UE_LOG(LogClass, Log, TEXT("獲取到 %d 個顯示設備\n"), iOutputNum);
for (size_t n = 0; n < vOutputs.Num(); 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);
UE_LOG(LogClass, Log, TEXT("顯示設備名稱: %s \n"), outputDesc.DeviceName);
UE_LOG(LogClass, Log, TEXT("顯示設備當前分辨率: %d * %d \n"), outputDesc.DesktopCoordinates.right - outputDesc.DesktopCoordinates.left, outputDesc.DesktopCoordinates.bottom - outputDesc.DesktopCoordinates.top);
// 所支持的分辨率信息
UE_LOG(LogClass, Log, TEXT("分辨率信息:"));
for (UINT m = 0; m < uModeNum; m++)
{
UE_LOG(LogClass, Log, TEXT("== 分辨率: %d * %d 刷新率: %d\n"), pModeDescs[m].Width, pModeDescs[m].Width, (pModeDescs[m].RefreshRate.Numerator) / (pModeDescs[m].RefreshRate.Denominator));
}
}
vOutputs.Empty();
}
vAdapters.Empty();
return 0;
}