C++如何:枚举disk,每个disk的total size、free size

ConnorWang2012 2014-02-13 08:14:16
大家好,用C++怎么枚举disk,以及每个disk的total size、free size?
Thanks~
...全文
306 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-02-14
  • 打赏
  • 举报
回复
GetLogicalDriveStrings The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system. DWORD GetLogicalDriveStrings( DWORD nBufferLength, // size of buffer LPTSTR lpBuffer // pointer to buffer for drive strings ); Parameters nBufferLength Specifies the maximum size, in characters, of the buffer pointed to by lpBuffer. This size does not include the terminating null character. lpBuffer Pointer to a buffer that receives a series of null-terminated strings, one for each valid drive in the system, that end with a second null character. The following example shows the buffer contents with <null> representing the terminating null character. c:\<null>d:\<null><null> Return Values If the function succeeds, the return value is the length, in characters, of the strings copied to the buffer, not including the terminating null character. Note that an ANSI-ASCII null character uses one byte, but a Unicode null character uses two bytes. If the buffer is not large enough, the return value is greater than nBufferLength. It is the size of the buffer required to hold the drive strings. If the function fails, the return value is zero. To get extended error information, use the GetLastError function. Remarks Each string in the buffer may be used wherever a root directory is required, such as for the GetDriveType and GetDiskFreeSpace functions. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also File I/O Overview, File Functions, GetDriveType, GetDiskFreeSpace, GetLogicalDrives GetDiskFreeSpace The GetDiskFreeSpace function retrieves information about the specified disk, including the amount of free space on the disk. This function has been superseded by the GetDiskFreeSpaceEx function. New Win32-based applications should use GetDiskFreeSpaceEx. BOOL GetDiskFreeSpace( LPCTSTR lpRootPathName, // pointer to root path LPDWORD lpSectorsPerCluster, // pointer to sectors per cluster LPDWORD lpBytesPerSector, // pointer to bytes per sector LPDWORD lpNumberOfFreeClusters, // pointer to number of free clusters LPDWORD lpTotalNumberOfClusters // pointer to total number of clusters ); Parameters lpRootPathName Pointer to a null-terminated string that specifies the root directory of the disk to return information about. If lpRootPathName is NULL, the function uses the root of the current directory. If this parameter is a UNC name, you must follow it with an additional backslash. For example, you would specify \\MyServer\MyShare as \\MyServer\MyShare\. Windows 95: The initial release of Windows 95 does not support UNC paths for the lpszRootPathName parameter. To query the free disk space using a UNC path, temporarily map the UNC path to a drive letter, query the free disk space on the drive, then remove the temporary mapping. Windows 95 OSR2 and later: UNC paths are supported. lpSectorsPerCluster Pointer to a variable for the number of sectors per cluster. lpBytesPerSector Pointer to a variable for the number of bytes per sector. lpNumberOfFreeClusters Pointer to a variable for the total number of free clusters on the disk that are available to the user associated with the calling thread. Windows NT 5.0 and later: If per-user disk quotas are in use, this value may be less than the total number of free clusters on the disk. lpTotalNumberOfClusters Pointer to a variable for the total number of clusters on the disk that are available to the user associated with the calling thread. Windows NT 5.0 and later: If per-user disk quotas are in use, this value may be less than the total number of clusters on the disk. Return Values If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks The GetDiskFreeSpaceEx function lets you avoid the arithmetic required by the GetDiskFreeSpace function. Windows 95: The GetDiskFreeSpace function returns incorrect values for volumes that are larger than 2 gigabytes. The function caps the values stored into *lpNumberOfFreeClusters and *lpTotalNumberOfClusters so as to never report volume sizes that are greater than 2 gigabytes. Even on volumes that are smaller than 2 gigabytes, the values stored into *lpSectorsPerCluster, *lpNumberOfFreeClusters, and *lpTotalNumberOfClusters values may be incorrect. That is because the operating system manipulates the values so that computations with them yield the correct volume size. Windows 95 OSR2 and later: The GetDiskFreeSpaceEx function is available on Windows 95 systems beginning with OEM Service Release 2 (OSR2). The GetDiskFreeSpaceEx function returns correct values for all volumes, including those that are greater than 2 gigabytes. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also File I/O Overview, File Functions, GetDiskFreeSpaceEx, GetDriveType
xiaohuh421 2014-02-14
  • 打赏
  • 举报
回复
http://download.csdn.net/detail/xiaohuh421/2682359
mujiok2003 2014-02-14
  • 打赏
  • 举报
回复
不要找C++, 找系统API去。
ConnorWang2012 2014-02-14
  • 打赏
  • 举报
回复
引用 1 楼 xiaohuh421 的回复:
http://download.csdn.net/detail/xiaohuh421/2682359
hi,xiaohuh421 ,thanks,还没试,不知道可不可以。
ConnorWang2012 2014-02-14
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
GetLogicalDriveStrings The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system. DWORD GetLogicalDriveStrings( DWORD nBufferLength, // size of buffer LPTSTR lpBuffer // pointer to buffer for drive strings ); Parameters nBufferLength Specifies the maximum size, in characters, of the buffer pointed to by lpBuffer. This size does not include the terminating null character. lpBuffer Pointer to a buffer that receives a series of null-terminated strings, one for each valid drive in the system, that end with a second null character. The following example shows the buffer contents with <null> representing the terminating null character. c:\<null>d:\<null><null> Return Values If the function succeeds, the return value is the length, in characters, of the strings copied to the buffer, not including the terminating null character. Note that an ANSI-ASCII null character uses one byte, but a Unicode null character uses two bytes. If the buffer is not large enough, the return value is greater than nBufferLength. It is the size of the buffer required to hold the drive strings. If the function fails, the return value is zero. To get extended error information, use the GetLastError function. Remarks Each string in the buffer may be used wherever a root directory is required, such as for the GetDriveType and GetDiskFreeSpace functions. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also File I/O Overview, File Functions, GetDriveType, GetDiskFreeSpace, GetLogicalDrives GetDiskFreeSpace The GetDiskFreeSpace function retrieves information about the specified disk, including the amount of free space on the disk. This function has been superseded by the GetDiskFreeSpaceEx function. New Win32-based applications should use GetDiskFreeSpaceEx. BOOL GetDiskFreeSpace( LPCTSTR lpRootPathName, // pointer to root path LPDWORD lpSectorsPerCluster, // pointer to sectors per cluster LPDWORD lpBytesPerSector, // pointer to bytes per sector LPDWORD lpNumberOfFreeClusters, // pointer to number of free clusters LPDWORD lpTotalNumberOfClusters // pointer to total number of clusters ); Parameters lpRootPathName Pointer to a null-terminated string that specifies the root directory of the disk to return information about. If lpRootPathName is NULL, the function uses the root of the current directory. If this parameter is a UNC name, you must follow it with an additional backslash. For example, you would specify \\MyServer\MyShare as \\MyServer\MyShare\. Windows 95: The initial release of Windows 95 does not support UNC paths for the lpszRootPathName parameter. To query the free disk space using a UNC path, temporarily map the UNC path to a drive letter, query the free disk space on the drive, then remove the temporary mapping. Windows 95 OSR2 and later: UNC paths are supported. lpSectorsPerCluster Pointer to a variable for the number of sectors per cluster. lpBytesPerSector Pointer to a variable for the number of bytes per sector. lpNumberOfFreeClusters Pointer to a variable for the total number of free clusters on the disk that are available to the user associated with the calling thread. Windows NT 5.0 and later: If per-user disk quotas are in use, this value may be less than the total number of free clusters on the disk. lpTotalNumberOfClusters Pointer to a variable for the total number of clusters on the disk that are available to the user associated with the calling thread. Windows NT 5.0 and later: If per-user disk quotas are in use, this value may be less than the total number of clusters on the disk. Return Values If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks The GetDiskFreeSpaceEx function lets you avoid the arithmetic required by the GetDiskFreeSpace function. Windows 95: The GetDiskFreeSpace function returns incorrect values for volumes that are larger than 2 gigabytes. The function caps the values stored into *lpNumberOfFreeClusters and *lpTotalNumberOfClusters so as to never report volume sizes that are greater than 2 gigabytes. Even on volumes that are smaller than 2 gigabytes, the values stored into *lpSectorsPerCluster, *lpNumberOfFreeClusters, and *lpTotalNumberOfClusters values may be incorrect. That is because the operating system manipulates the values so that computations with them yield the correct volume size. Windows 95 OSR2 and later: The GetDiskFreeSpaceEx function is available on Windows 95 systems beginning with OEM Service Release 2 (OSR2). The GetDiskFreeSpaceEx function returns correct values for all volumes, including those that are greater than 2 gigabytes. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also File I/O Overview, File Functions, GetDiskFreeSpaceEx, GetDriveType
Hi zhao4zhong1,似乎GetLogicalDriveStrings、GetDiskFreeSpaceEx等函数只能对分区后拥有drive letter的drive操作,若一个disk没有分区,没有drive letter就没办法使用了。 另外,通过获取一个disk的每个drive的size,再加起来得到一个disk的total size是不是太麻烦? 解决方法: 枚举disk以及disk的size等信息,可以通过CreateFile + DeviceIoControl的方法,sample code:

BOOL EnumDisks(DWORD *nDisks)
{
	BOOL bRet = true;
	DWORD dwCountDisks = 0;
	HANDLE hFile; 
	TCHAR chDriverName[32];

	try
	{
		for(int i = 0;i < MAX_DISK_NUMBER;i++)
		{
			_stprintf_s(chDriverName,_T("\\\\.\\PhysicalDrive%d"),i);
			hFile = CreateFile(	chDriverName,
								GENERIC_READ,
								0,	// FILE_SHARE_READ|FILE_SHARE_WRITE,
								// 0:prevents other processes access this file
								NULL,
								OPEN_EXISTING,
								0,
								NULL);
			if(hFile == INVALID_HANDLE_VALUE)
				break;

			++dwCountDisks;  

			if(hFile != NULL)
				CloseHandle(hFile);
		}

		*nDisks = dwCountDisks;
	}
	catch(...)
	{
		bRet = false;
	}

	return bRet;
}

DWORD GetDiskSize(WORD wDiskIndex ,LONGLONG *nOutBytes)
{
	BOOL bResult;
	DWORD dwOutBytes;
	HANDLE hFile;
	PDISK_GEOMETRY_EX pdge;
	TCHAR chDriverName[32];

	pdge = (PDISK_GEOMETRY_EX)new BYTE[sizeof(DISK_GEOMETRY_EX)];
	_stprintf_s(chDriverName,_T("\\\\.\\PhysicalDrive%d"),wDiskIndex);

	hFile = CreateFile(	chDriverName,
						GENERIC_READ,
						0,	// FILE_SHARE_READ|FILE_SHARE_WRITE,
						// 0:prevents other processes access this file
						NULL,
						OPEN_EXISTING,
						0,
						NULL);

	if(hFile == INVALID_HANDLE_VALUE)
		return 1;

	bResult = DeviceIoControl(  hFile,
								IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
								NULL,
								0,
								pdge,
								sizeof(DISK_GEOMETRY_EX),
								&dwOutBytes,
								(LPOVERLAPPED)NULL);
	if (!bResult)
		return 2;

	*nOutBytes = pdge->DiskSize.QuadPart;

	if(hFile != NULL)
		CloseHandle(hFile);

	return 0;
}
Thanks,zhao4zhong1

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧