[c++] 获取磁盘信息(磁盘驱号和内存使用情况)

版权声明:Copyright (c) 2018 初阳-.-#.Co.Ltd. All rights reserved. https://blog.csdn.net/xwh012510/article/details/86230873

VS2017 使用Unicode 字符集写的一个例子---获取磁盘标号,大小,使用情况。

GetDriveType

头文件在“winbase.h"

判断一个磁盘驱动器的类型,返回值long,如果不能识别,则返回0.若是指定目录不存在,则返回1,如执行成功,则用下述任何一个常数指定驱动器类型:DRIVE_REMOVABLE, DRIVE_FIXED, DRIVE_REMOTE, DRIVE_CDROM 或 DRIVE_RAMDISK

函数原型如下:

UINT GetDriveType(

LPCTSTR lpRootPathName // root directory

);

参数说明: lpRootPathName 包含了根目录路径的字符串指针

返回值

DRIVE_UNKNOWN 未知的磁盘类型

DRIVE_NO_ROOT_DIR 说明lpRootPathName是无效的

DRIVE_REMOVABLE 可移动磁盘

DRIVE_FIXED 固定磁盘

DRIVE_REMOTE 网络磁盘

DRIVE_CDROM 光驱

DRIVE_RAMDISK 为RAM

GetLogicalDriveStrings,获取一个字串,其中包含了当前所有逻辑驱动器的根驱动器路径。

函数原型如下:
DWORD GetLogicalDriveStrings(
DWORD nBufferLength,
LPTSTR lpBuffer);
参数:
nBufferLength: 指向的内存空间的大小,以字节为单位。
lpBuffer: 指向存储返回结果字符串的内存空间
返回值:
函数的返回值指明了函数调用是否成功,如果成功则返回缓冲区中返回结果的总长度;如果返回值大于nBufferLength,说明给定的缓冲区大小不够,返回值是实际需要的大小;如果返回0,则说明函数运行出错。
说明:
函数调用成功后,将在缓冲区中依次填入本机所具有的驱动器根路径字符串,假如系统中有4个逻辑驱动器“C:\”、“D:\”、“E:\”,“F:\”。执行后在缓冲区中的结果如下:
0x43 0x3a 0x5c 0x00 0x44 0x3a 0x5c 0x000x45 0x3a 0x5c 0x00 0x46 0x3a 0x5c 0x00 0x00

即连续存放了“C:\”、“D:\”、“E:\”,“F:\”这4个字符串。

注意:会在每个字符串后加一个‘\0’结束符,在所有卷标字符串的最后在加一个结束符)。

GetDiskFreeSpaceEx

#ifdef UNICODE---GetDiskFreeSpaceExW

#else---GetDiskFreeSpaceExA

在vs中函数原型如下:

WINBASEAPI
BOOL
WINAPI
GetDiskFreeSpaceExA(
    _In_opt_ LPCSTR lpDirectoryName,
    _Out_opt_ PULARGE_INTEGER lpFreeBytesAvailableToCaller,
    _Out_opt_ PULARGE_INTEGER lpTotalNumberOfBytes,
    _Out_opt_ PULARGE_INTEGER lpTotalNumberOfFreeBytes
    );

WINBASEAPI
BOOL
WINAPI
GetDiskFreeSpaceExW(
    _In_opt_ LPCWSTR lpDirectoryName,
    _Out_opt_ PULARGE_INTEGER lpFreeBytesAvailableToCaller,
    _Out_opt_ PULARGE_INTEGER lpTotalNumberOfBytes,
    _Out_opt_ PULARGE_INTEGER lpTotalNumberOfFreeBytes
    );

lpDirectoryName是驱动器的名称。

lpFreeBytesAvailableToCaller是用户可用的磁盘空间。

lpTotalNumberOfBytes是磁盘总共的空间。

lpTotalNumberOfFreeBytes是磁盘空闲的空间。以上都是字节为单位。

. cpp

#include <iostream>
#include <windows.h>
#include <string.h>

#define GB(x) (x.HighPart << 2) + (x.LowPart >> 20) / 1024.0

using namespace std;

int main()
{
	ULARGE_INTEGER freeBytesAvailableToCaller;
	ULARGE_INTEGER totalNumberOfBytes;
	ULARGE_INTEGER totalNumberOfFreeBytes;
	int totalOfGB, freeOfGB, useOfGB;
	string drive_cout, drive_index, total_size, free_size;

	DWORD drive_info = GetLogicalDrives();	//获取主机中所有的逻辑驱动器字母,以BitMap的形式返回
	char driver_info_buf[MAX_PATH] = { 0 };
	_itoa_s(drive_info, driver_info_buf, 2);
	int m_driverCount = 0;
	while (drive_info) 
	{
		if (drive_info & 1) 
		{
			m_driverCount++;
		}
		drive_info >>= 1;
	}
	cout << "磁盘个数: " << m_driverCount << endl;
	int drive_strLen = GetLogicalDriveStrings(0, NULL);	//获取一个字串,其中包含了当前所有逻辑驱动器的根驱动器路径。
	WCHAR *drive_str = new WCHAR[drive_strLen];
	GetLogicalDriveStrings(drive_strLen, drive_str);	//装载逻辑驱动器名称的字串到数组driveStr
	for (int i = 0; i < m_driverCount; i++)
	{
		UINT driveType = GetDriveType(drive_str);
		GetDiskFreeSpaceEx(drive_str, &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes);

		if (driveType != DRIVE_FIXED)	//固定磁盘
			return 0;

		string disk_name((const char*)drive_str);
		drive_index = disk_name.substr(0, 2);		
		totalOfGB = GB(totalNumberOfBytes);
		freeOfGB = GB(totalNumberOfFreeBytes);
		useOfGB = totalOfGB - freeOfGB;

		cout << "磁盘 " << drive_index.c_str() << " 总大小: " << totalOfGB << " GB  可用空间: " << freeOfGB << " GB  已使用: " << useOfGB << " GB" << endl;
		drive_str += 4;
	}
	return 0;
}

方法二

GlobalMemoryStatusEx函数用于获取系统内存信息:

BOOL WINAPI GlobalMemoryStatusEx(
  __inout  LPMEMORYSTATUSEX lpBuffer
);

//  Sample output:
//  There is       51 percent of memory in use.
//  There are 2029968 total KB of physical memory.
//  There are  987388 free  KB of physical memory.
//  There are 3884620 total KB of paging file.
//  There are 2799776 free  KB of paging file.
//  There are 2097024 total KB of virtual memory.
//  There are 2084876 free  KB of virtual memory.
//  There are       0 free  KB of extended memory.

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

// Use to convert bytes to KB
#define DIV 1024

// Specify the width of the field in which to print the numbers. 
// The asterisk in the format specifier "%*I64d" takes an integer 
// argument and uses it to pad and right justify the number.
#define WIDTH 7

void _tmain()
{
  MEMORYSTATUSEX statex;

  statex.dwLength = sizeof (statex);

  GlobalMemoryStatusEx (&statex);

  _tprintf (TEXT("There is  %*ld percent of memory in use.\n"),
            WIDTH, statex.dwMemoryLoad);
  _tprintf (TEXT("There are %*I64d total KB of physical memory.\n"),
            WIDTH, statex.ullTotalPhys/DIV);
  _tprintf (TEXT("There are %*I64d free  KB of physical memory.\n"),
            WIDTH, statex.ullAvailPhys/DIV);
  _tprintf (TEXT("There are %*I64d total KB of paging file.\n"),
            WIDTH, statex.ullTotalPageFile/DIV);
  _tprintf (TEXT("There are %*I64d free  KB of paging file.\n"),
            WIDTH, statex.ullAvailPageFile/DIV);
  _tprintf (TEXT("There are %*I64d total KB of virtual memory.\n"),
            WIDTH, statex.ullTotalVirtual/DIV);
  _tprintf (TEXT("There are %*I64d free  KB of virtual memory.\n"),
            WIDTH, statex.ullAvailVirtual/DIV);

  // Show the amount of extended memory available.

  _tprintf (TEXT("There are %*I64d free  KB of extended memory.\n"),
            WIDTH, statex.ullAvailExtendedVirtual/DIV);
}

方法二借鉴于MSDN:https://docs.microsoft.com/zh-cn/windows/desktop/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex

猜你喜欢

转载自blog.csdn.net/xwh012510/article/details/86230873