VC++获取磁盘驱动器卷标和序列号并将之存储到CStringList中

获取磁盘驱动器主要使用GetLogicalDriveStrings()函数,获取磁盘序列号主要使用GetVolumeInformation()函数。需要提醒一下大家的是,CStringList没有重载操作符“=”,故不能直接将CStringList作为返回值或者函数参数,应该使用“引用”或者“指针”的方式。

 

// 获取磁盘驱动器
void CGetDiskSerialDlg::GetDiskDriverVolumeLabel(CStringList &volumeLabelList)
{
	DWORD size = ::GetLogicalDriveStrings(0, NULL);
	if (size != 0)
	{
		HANDLE heap = ::GetProcessHeap();
		LPSTR lp = (LPSTR)HeapAlloc(heap, HEAP_ZERO_MEMORY, size * sizeof(TCHAR));
		::GetLogicalDriveStrings(size * sizeof(TCHAR), lp);

		while (*lp != 0)
		{
			volumeLabelList.AddTail(lp);
			lp = _tcschr(lp, 0) + 1;
		}
	}
	return;
}

// 由卷标获取磁盘序列号
void CGetDiskSerialDlg::GetDiskSerial(CStringList &diskSerialList, CStringList &volumeLabelList)
{	
	// 获取序列号
	LPTSTR namebuf = new char[12];
	DWORD namesize = 12;

	DWORD serialnumber;
	DWORD maxlen;
	DWORD fileflag;

	LPTSTR sysnamebuf = new char[10];
	DWORD sysnamesize = 10;

	if ( !volumeLabelList.IsEmpty() )
	{
		int num = volumeLabelList.GetCount(); // 获取磁盘数量
		for (int i = 0; i < num; i++)
		{
			CString volumeLabel, strDiskSerial;
			volumeLabel = volumeLabelList.GetAt(volumeLabelList.FindIndex(i));

			::GetVolumeInformation(volumeLabel, namebuf, namesize, &serialnumber, &maxlen, &fileflag, sysnamebuf, sysnamesize);
			strDiskSerial.Format("%x", serialnumber);
			diskSerialList.AddTail(strDiskSerial);
		}
	}

	return;
}

 

另外附上将信息设置到CListCtrl控件上的代码吧,也就是这两个函数具体的使用方法示例。

CStringList VolumeLabelList;
CStringList DiskSerialList;
GetDiskDriverVolumeLabel(VolumeLabelList);

if ( !VolumeLabelList.IsEmpty() )
{
	GetDiskSerial(DiskSerialList, VolumeLabelList);
	if ( !DiskSerialList.IsEmpty() )
	{
		for (int i = 0; i < VolumeLabelList.GetCount(); i++)
		{
			m_list_diskSerial.InsertItem(i, VolumeLabelList.GetAt(VolumeLabelList.FindIndex(i)));
			m_list_diskSerial.SetItemText(i, 1, DiskSerialList.GetAt(DiskSerialList.FindIndex(i)));
		}
	}
}

 

/*============================================================================================*/

PS:如果不将磁盘驱动器和序列号存储到CStringList中,而仅仅只是显示到CListCtrl控件上,那么可以简化成一个函数:

        首先,初始化CListCtrl控件,这里我将控件变量命名为m_list_diskSerial:

m_list_diskSerial.SetExtendedStyle(LVS_EX_GRIDLINES);
m_list_diskSerial.InsertColumn(0, _T("磁盘驱动器"), LVCFMT_LEFT, 150);
m_list_diskSerial.InsertColumn(0, _T("磁盘序列号"), LVCFMT_LEFT, 150);

        接着,在类中声明GetDiskSerial()函数,先获取卷标并设置在CListCtrl控件上,接着从控件上获取卷标并由获取到的卷标获取磁盘序列号。

// 获取磁盘序列号
void CGetDiskSerialDlg::GetDiskSerial()
{
	// 获取驱动器信息
	DWORD size = ::GetLogicalDriveStrings(0, NULL);
	if (size != 0)
	{
		HANDLE heap = ::GetProcessHeap();
		LPSTR lp = (LPSTR)HeapAlloc(heap, HEAP_ZERO_MEMORY, size * sizeof(TCHAR));
		::GetLogicalDriveStrings(size * sizeof(TCHAR), lp);

		while (*lp != 0)
		{
			m_list_diskSerial.InsertItem(0, lp, 0);
			lp = _tcschr(lp, 0) + 1;
		}
	}

	// 获取序列号
	LPTSTR namebuf = new char[12];
	DWORD namesize = 12;

	DWORD serialnumber;
	DWORD maxlen;
	DWORD fileflag;

	LPTSTR sysnamebuf = new char[10];
	DWORD sysnamesize = 10;

	int num = m_list_diskSerial.GetItemCount(); // 获取磁盘数量
	for (int i = 0; i < num; i++)
	{
		CString volumeLabel, strDiskSerial;
		volumeLabel = m_list_diskSerial.GetItemText(i, 0);

		::GetVolumeInformation(volumeLabel, namebuf, namesize, &serialnumber, &maxlen, &fileflag, sysnamebuf, sysnamesize);
		strDiskSerial.Format("%x", serialnumber);
		m_list_diskSerial.SetItemText(i, 1, strDiskSerial);
	}
}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页