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

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在C语言中,要获取磁盘卷标名称,可以使用以下步骤: 1. 首先,需要包含Windows.h头文件,这个头文件中定义了一些用于Windows系统编程的函数和数据结构。 2. 使用GetVolumeInformation函数来获取磁盘卷标名称。该函数的原型如下: BOOL GetVolumeInformationA( LPCSTR lpRootPathName, LPSTR lpVolumeNameBuffer, DWORD nVolumeNameSize, LPDWORD lpVolumeSerialNumber, LPDWORD lpMaximumComponentLength, LPDWORD lpFileSystemFlags, LPSTR lpFileSystemNameBuffer, DWORD nFileSystemNameSize ); 其中,lpRootPathName是一个指向包含卷标的根路径的字符串,比如"C:\";lpVolumeNameBuffer是用来存放卷标名称的缓冲区;nVolumeNameSize是缓冲区大小;其他的参数可以传入NULL。 3. 创建一个字符数组,用来存储卷标名称。 4. 调用GetVolumeInformation函数,并传入磁盘根路径和字符数组作为参数。 5. 如果函数返回TRUE,表示获取卷标名称成功,可以通过打印字符数组的值来查看卷标名称。 以下是一个示例代码: ```c #include <Windows.h> #include <stdio.h> int main() { char volumeName[MAX_PATH]; char rootPath[] = "C:\\"; if (GetVolumeInformationA(rootPath, volumeName, sizeof(volumeName), NULL, NULL, NULL, NULL, 0)) { printf("磁盘卷标名称: %s\n", volumeName); } else { printf("无法获取磁盘卷标名称\n"); } return 0; } ``` 以上是用C语言获取磁盘卷标名称的方法。注意,在Windows系统上,不是所有的磁盘都会有卷标名称,所以有可能获取到的名称为空。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初阳-.-#

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值