关闭

获取硬盘名字

297人阅读 评论(0) 收藏 举报
分类:
#include <windows.h>
#include <stdio.h>
#include <initguid.h>
#include <setupapi.h>
#pragma comment(lib,"setupapi.lib")



int main(void)
{

	
	for (int i = 0;; i++)
	{
		GUID objGuid = { 0 };
		DWORD  dwSize = 0;
		//'获取类名为"Ports"的GUID
		if (SetupDiClassGuidsFromName("DiskDrive", &objGuid, 1, &dwSize) == FALSE)
		{
			int a = GetLastError();
			return 0;
		}

		//'根据串口GUID获取设备句柄
		HDEVINFO 	hDevInfo = SetupDiGetClassDevs(&objGuid, 0, 0, DIGCF_PRESENT | DIGCF_PROFILE);
		if (INVALID_HANDLE_VALUE == hDevInfo)
		{
			int a = GetLastError();
			return 0;
		}

		//'根据设备句柄检举包含的设备
		SP_DEVINFO_DATA  objSpdd = { 0 };
		objSpdd.cbSize = sizeof(objSpdd);

		if (SetupDiEnumDeviceInfo(hDevInfo, i, &objSpdd) == FALSE)
		{
			int a = GetLastError();
			return 0;
		}

		// 根据dwIndex设备句柄请求FRIENDLYNAME访问
		BYTE PropertyBuffer[MAX_PATH] = { 0 };
		if (SetupDiGetDeviceRegistryProperty(hDevInfo, &objSpdd, SPDRP_FRIENDLYNAME, 0, PropertyBuffer, MAX_PATH, &dwSize) == FALSE)
		{
			int a = GetLastError();
			return 0;
		}
		printf("%s\n", PropertyBuffer);

	}
	/*
	ST1000DM003-1SB10C
	INTEL SSDSC2BW240H6
	INTEL SSDSC2KW120H6
	TOSHIBA USB FLASH DRIVE USB Device
	请按任意键继续. . .
	*/

	getchar();
	return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场