UE4获取硬件显卡信息

.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include <string>
#include "XiankaActor.generated.h"

using namespace std;
UCLASS()
class AA418_API AXiankaActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AXiankaActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION(BlueprintCallable, Category = "DCFUtil")
		static void GetDisplayCardInfo();
	static string WStringToString(const wstring &wstr);

	/****获取显卡信息*****/
	UFUNCTION(BlueprintCallable, Category = "DCFUtil")
		static int GetDisplayCardInfo2();
};

.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "XiankaActor.h"
#include <DXGI.h>  

// Sets default values
AXiankaActor::AXiankaActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AXiankaActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AXiankaActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void AXiankaActor::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();
	}
}

std::string AXiankaActor::WStringToString(const wstring &wstr)
{
	std::string str(wstr.length(), ' ');
	std::copy(wstr.begin(), wstr.end(), str.begin());
	return str;
}

int AXiankaActor::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;
}

转:https://blog.csdn.net/baidu_28938713/article/details/79238194

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页