C++读设备管理器信息 计算机管理信息 串口列表、描述信息等 只显示存在的串口源码 解决中文乱码

C++读设备管理器信息 计算机管理信息 串口列表、描述信息等 只显示存在的串口源码 解决中文乱码,第1张

C++读设备管理器信息 计算机管理信息 串口列表、描述信息等 只显示存在的串口源码 解决中文乱码

// PrintDeviceInfo.cpp : 定义控制台应用程序的入口点。
//
#include 
#include 
#include 
#include 
#pragma comment(lib, "setupapi.lib")

void  PrintDevicesInfo1()
{
	HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES);
	if (hDevInfo == INVALID_HANDLE_VALUE)
	{
		printf("SetupDiGetClassDevs Err:%d", GetLastError());
		return;
	};

	SP_CLASSIMAGELIST_DATA _spImageData = { 0 };
	_spImageData.cbSize = sizeof(SP_CLASSIMAGELIST_DATA);
	SetupDiGetClassImageList(&_spImageData);

	SP_DEVINFO_DATA spDevInfoData = { 0 };
	spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
	for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)	
	{
		char  szBuf[MAX_PATH] = { 0 };
		int  wImageIdx = 0;
		short  wItem = 0;
		if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &spDevInfoData, SPDRP_CLASS, NULL, (PBYTE)szBuf, MAX_PATH, 0))
		{
			continue;
		};

		if (SetupDiGetClassImageIndex(&_spImageData, &spDevInfoData.ClassGuid, &wImageIdx))
		{
			char  szName[MAX_PATH] = { 0 };
			DWORD  dwRequireSize;
				
			if (!SetupDiGetClassDescription(&spDevInfoData.ClassGuid, (PWSTR)szBuf, MAX_PATH, &dwRequireSize))
			{
				continue;
			};
			wprintf(L"Class:%srn", szBuf);

			if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)szName, MAX_PATH - 1, 0))
			{
				wprintf(L"FriendlyName:%srnrn", szName);
			}
			else  if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szName, MAX_PATH - 1, 0))
			{
				wprintf(L"Device:%srnrn", szName);
			}
		}	
	}
	SetupDiDestroyClassImageList(&_spImageData);

}


int PrintDevicesInfo2()
{
	// 得到所有设备 HDEVINFO    
	HDEVINFO hDevInfo;
	hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES);
	if (hDevInfo == INVALID_HANDLE_VALUE)
	{
		wprintf(L"SetupDiGetClassDevs Err:%d", GetLastError());
		return -1;
	};

	// 循环列举   
	SP_DEVINFO_DATA DeviceInfoData;
	DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
	for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
	{
		WCHAR   szClassBuf[MAX_PATH] = { 0 };
		WCHAR   szDescBuf[MAX_PATH] = { 0 };
		WCHAR   locinfo[MAX_PATH] = { 0 };
		WCHAR   friendName[MAX_PATH] = { 0 };
		// 获取类名  
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS, NULL, (PBYTE)szClassBuf, MAX_PATH - 1, NULL))
		{
		}
		//获取设备描述信息
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szDescBuf, MAX_PATH - 1, NULL))
		{
		}
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_LOCATION_INFORMATION, NULL, (PBYTE)locinfo, sizeof(locinfo), NULL))
		{
		}
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)friendName, sizeof(friendName), NULL))
		{
		}
		wprintf(L"Class:%srnDesc:%snLocoInfo:%snFriendName:%srnrn", szClassBuf, szDescBuf, locinfo,friendName);
	}

	//  释放     
	SetupDiDestroyDeviceInfoList(hDevInfo);
	return 0;
}

int  main(int  argc, char* argv[])
{
	printf("Locale is: %sn", setlocale(LC_ALL, "chs"));
	PrintDevicesInfo1();
	//PrintDevicesInfo2();
	getchar();
	return  0;
}
只显示存在的串口

 

// PrintDeviceInfo.cpp : 定义控制台应用程序的入口点。
//
#include 
#include 
#include 
#include 
#pragma comment(lib, "setupapi.lib")

void  PrintDevicesInfo1()
{
	HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);
	if (hDevInfo == INVALID_HANDLE_VALUE)
	{
		printf("SetupDiGetClassDevs Err:%d", GetLastError());
		return;
	};

	SP_CLASSIMAGELIST_DATA _spImageData = { 0 };
	_spImageData.cbSize = sizeof(SP_CLASSIMAGELIST_DATA);
	SetupDiGetClassImageList(&_spImageData);

	SP_DEVINFO_DATA spDevInfoData = { 0 };
	spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
	for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)	
	{
		char  szBuf[MAX_PATH] = { 0 };
		int  wImageIdx = 0;
		short  wItem = 0;
		if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &spDevInfoData, SPDRP_CLASS, NULL, (PBYTE)szBuf, MAX_PATH, 0))
		{
			continue;
		};
		if (strcmp(szBuf, "Ports") != 0)
		{
			continue;
		}
		printf("Class1:%srn", szBuf);
		if (SetupDiGetClassImageIndex(&_spImageData, &spDevInfoData.ClassGuid, &wImageIdx))
		{
			char  szName[MAX_PATH] = { 0 };
			DWORD  dwRequireSize;
				
			if (!SetupDiGetClassDescription(&spDevInfoData.ClassGuid, (PWSTR)szBuf, MAX_PATH, &dwRequireSize))
			{
				continue;
			};
			wprintf(L"Class:%srn", szBuf);

			if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)szName, MAX_PATH - 1, 0))
			{
				wprintf(L"FriendlyName:%srnrn", szName);
			}
			else  if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szName, MAX_PATH - 1, 0))
			{
				wprintf(L"Device:%srnrn", szName);
			}
		}	
	}
	SetupDiDestroyClassImageList(&_spImageData);

}


int PrintDevicesInfo2()
{
	// 得到所有设备 HDEVINFO    
	HDEVINFO hDevInfo;
	hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES);
	if (hDevInfo == INVALID_HANDLE_VALUE)
	{
		wprintf(L"SetupDiGetClassDevs Err:%d", GetLastError());
		return -1;
	};

	// 循环列举   
	SP_DEVINFO_DATA DeviceInfoData;
	DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
	for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
	{
		WCHAR   szClassBuf[MAX_PATH] = { 0 };
		WCHAR   szDescBuf[MAX_PATH] = { 0 };
		WCHAR   locinfo[MAX_PATH] = { 0 };
		WCHAR   friendName[MAX_PATH] = { 0 };
		// 获取类名  
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS, NULL, (PBYTE)szClassBuf, MAX_PATH - 1, NULL))
		{
		}
		//获取设备描述信息
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szDescBuf, MAX_PATH - 1, NULL))
		{
		}
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_LOCATION_INFORMATION, NULL, (PBYTE)locinfo, sizeof(locinfo), NULL))
		{
		}
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)friendName, sizeof(friendName), NULL))
		{
		}
		wprintf(L"Class:%srnDesc:%snLocoInfo:%snFriendName:%srnrn", szClassBuf, szDescBuf, locinfo,friendName);
	}

	//  释放     
	SetupDiDestroyDeviceInfoList(hDevInfo);
	return 0;
}

int  main(int  argc, char* argv[])
{
	printf("Locale is: %sn", setlocale(LC_ALL, "chs"));
	PrintDevicesInfo1();
	//PrintDevicesInfo2();
	getchar();
	return  0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5703239.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存