windows获取系统显卡信息

Posted 朱韦刚

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows获取系统显卡信息相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <windows.h>
#include <iostream>  
#include <DXGI.h>  
#include <vector>  

#pragma comment(lib,"DXGI.lib") 
#pragma warning(disable: 4996)   


//获取操作系统信息
void GetOsInfo(char * pWindowsVersion,int * pWindowsOSBit)

	//获取操作系统版本
	char WindowsVersion[256] =  0 ;

	//获取8.1以上版本操作系统名称
	//先判断是否为win8.1或win10
	HMODULE hm = NULL;
	if (hm = LoadLibrary(TEXT("ntdll.dll")))
	
		DWORD dwMajor = 0, dwMinor = 0, dwBuildNumber = 0;
		typedef void (WINAPI *getver)(DWORD*, DWORD*, DWORD*);
		getver gv = NULL;
		gv = (getver)GetProcAddress(hm, "RtlGetNtVersionNumbers");
		if (gv)
		
			gv(&dwMajor, &dwMinor, &dwBuildNumber);
			//printf("Major is %d\\nMinor is %d\\nOptional Build is %d\\n", dwMajor, dwMinor, dwBuildNumber);
			if (dwMajor == 6 && dwMinor == 3) //win 8.1
			
				sprintf_s(WindowsVersion, "%s", "Windows 8.1");
			
			else if (dwMajor == 10 && dwMinor == 0) //win 10
			
				sprintf_s(WindowsVersion, "%s", "Windows 10");
			
		

		FreeLibrary(hm);

		//如果没有输出版本
		if (WindowsVersion[0] == 0)
		
			//获取8.1以下版本操作系统名称
			// get os name according to version number
			OSVERSIONINFO osver =  sizeof(OSVERSIONINFO) ;
			GetVersionEx(&osver);

			if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 0)
			
				sprintf_s(WindowsVersion, "%s", "Windows 2000");
			
			else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 1)
			
				sprintf_s(WindowsVersion, "%s", "Windows XP");
			
			else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 0)
			
				sprintf_s(WindowsVersion, "%s", "Windows 2003");
			
			else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2)
			
				sprintf_s(WindowsVersion, "%s", "Windows vista");
			
			else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 1)
			
				sprintf_s(WindowsVersion, "%s", "windows 7");
			
			else if ((osver.dwMajorVersion == 6 && osver.dwMinorVersion == 2) ||
				(osver.dwMajorVersion == 6 && osver.dwMinorVersion == 3))
			
				sprintf_s(WindowsVersion, "%s", "windows 8");
			
			else if ((osver.dwMajorVersion == 10 && osver.dwMinorVersion == 0)
				|| (osver.dwMajorVersion == 10))
			
				sprintf_s(WindowsVersion, "%s", "windows 10");
			
			else
			
				sprintf_s(WindowsVersion, "%s", "unkonw");
			
		
	
	else
	
		sprintf_s(WindowsVersion, "%s", "unkonw");
	

	memcpy(pWindowsVersion, WindowsVersion, strlen(WindowsVersion));
	pWindowsVersion[strlen(WindowsVersion)] = 0;

	//获取操作系统位数
	int WindowsOSBit = 0;
	SYSTEM_INFO si;
	// Copy the hardware information to the SYSTEM_INFO structure.
	GetNativeSystemInfo(&si);
	// Display the contents of the SYSTEM_INFO structure.
	if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 || si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
	
		WindowsOSBit = 64;
	
	else
	
		WindowsOSBit = 32;
	
	*pWindowsOSBit = WindowsOSBit;

	//printf("WindowsVersion : %s\\n", WindowsVersion);
	//printf("osver version : %ld.%ld\\n", osver.dwMajorVersion, osver.dwMinorVersion);
	//printf("WindowsOSBit : %d\\n",WindowsOSBit);
	//printf("si.wProcessorArchitecture = %d\\nPROCESSOR_ARCHITECTURE_AMD64 = %d\\nPROCESSOR_ARCHITECTURE_IA64 = %d\\n",
	//	si.wProcessorArchitecture, PROCESSOR_ARCHITECTURE_AMD64, PROCESSOR_ARCHITECTURE_IA64);

	return;


//WString转String
std::string WStringToString(const std::wstring &wstr)

	std::string str(wstr.length(), ' ');
	std::copy(wstr.begin(), wstr.end(), str.begin());
	return str;


//获取显卡信息
int GetGpuInfo(char * pDeviceDescription1, char * pDeviceDescription2, char * pDeviceDescription3, int* pDeviceDescriptionsize)

	// 参数定义  
	IDXGIFactory * pFactory;
	IDXGIAdapter * pAdapter;
	std::vector <IDXGIAdapter*> vAdapters;            // 显卡vector         
	int iAdapterNum = 0; // 显卡的数量  

	//创建一个DXGI工厂 (必须用CreateDXGIFactory1,如果用CreateDXGIFactory只能取出一块显卡信息) 
	HRESULT hr = CreateDXGIFactory1(__uuidof(IDXGIFactory), (void**)(&pFactory));
	if (FAILED(hr))
	
		return 0;
	

	// 枚举适配器  
	while (pFactory->EnumAdapters(iAdapterNum, &pAdapter) != DXGI_ERROR_NOT_FOUND)
	
		vAdapters.push_back(pAdapter);
		++iAdapterNum;
	
	*pDeviceDescriptionsize = iAdapterNum;

	// 信息输出  
	//printf("===============获取到%d块显卡===============\\n", iAdapterNum);
	for (size_t i = 0; i < vAdapters.size(); i++)
	
		// 获取信息  
		DXGI_ADAPTER_DESC adapterDesc;
		vAdapters[i]->GetDesc(&adapterDesc);
		std::wstring WDeviceDescription(adapterDesc.Description);
		std::string DeviceDescription = WStringToString(WDeviceDescription);

		if (i == 0)
		
			memcpy(pDeviceDescription1, DeviceDescription.c_str(), DeviceDescription.size());
			pDeviceDescription1[DeviceDescription.size()] = 0;
		
		if (i == 1)
		
			memcpy(pDeviceDescription2, DeviceDescription.c_str(), DeviceDescription.size());
			pDeviceDescription2[DeviceDescription.size()] = 0;
		
		if (i == 2)
		
			memcpy(pDeviceDescription3, DeviceDescription.c_str(), DeviceDescription.size());
			pDeviceDescription3[DeviceDescription.size()] = 0;
		
		
		// 输出显卡信息  
		//printf("显卡型号: %s\\n", DeviceDescription.c_str());
	
	vAdapters.clear();

	return 1;


int main()

	int ret = 0;
	//获取操作系统信息
	char nWindowsVersion[256] =  0 ;
	int nWindowsOSBit = 0;
	GetOsInfo(nWindowsVersion, &nWindowsOSBit);

	printf("nWindowsVersion : %s\\n", nWindowsVersion);
	printf("nWindowsOSBit : %d\\n", nWindowsOSBit);

	//获取显卡信息
	char nDeviceDescription1[256] =  0 ;
	char nDeviceDescription2[256] =  0 ;
	char nDeviceDescription3[256] =  0 ;
	int nDeviceDescription1size = 0;
	ret = GetGpuInfo(nDeviceDescription1, nDeviceDescription2, nDeviceDescription3, &nDeviceDescription1size);
	if (ret == 1)
	
		printf("nDeviceDescription1size :%d\\n", nDeviceDescription1size);
		printf("nDeviceDescription1 : %s\\n", nDeviceDescription1);
		if (strlen(nDeviceDescription2) != 0)
		
			printf("nDeviceDescription2 : %s\\n", nDeviceDescription2);
		
		if (strlen(nDeviceDescription3) != 0)
		
			printf("nDeviceDescription3 : %s\\n", nDeviceDescription3);
		
	
	else
	
		printf("GetGpuInfo error\\n");
	

	return getchar();

驱动程序版本号需要根据不同的驱动比如amd,intel,nvidia获取。

 

如有错误请指正:

交流请加QQ群:62054820
QQ:379969650.

以上是关于windows获取系统显卡信息的主要内容,如果未能解决你的问题,请参考以下文章

windows平台下 c++获取 系统版本 网卡 内存 CPU 硬盘 显卡信息

windows平台下 c++获取 系统版本 网卡 内存 CPU 硬盘 显卡信息

windows7怎样查看显卡

win11如何查看显卡信息?

C#实现Windows环境硬件检测工具

Windows查看显卡支持的CUDA版本号