windows 下C++获取磁盘信息笔记
来源:互联网 发布:excel2010编程教程 编辑:程序博客网 时间:2024/06/06 05:05
windows下获取磁盘信息:
头文件为:windows.h
1、获取系统逻辑驱动的数量:
使用 GetLogicalDrives(void) 函数获取逻辑驱动的数量,函数返回一个32无符号整型数据
DWORD diskCount = GetLogicalDrives()
可以通过对返回的无符号整型数据 1 的个数来判断磁盘是否为真,如果为 0 表示磁盘不存在
while(diskCount)
{
if(diskCount & 1)
{
++num;
}
diskCount = diskCount >> 1;
}
num 即为真实存在磁盘的数量
2、获取所有驱动器字符串信息
GetLogicalDriveStrings()函数 获取所有驱动器字符串信息
函数原型为:DWORD GetLogicalDriveStrings(
DWORD nBufferLength,
LPTSTR lpBuffer
);
nBufferLength 表示lpBuffer缓冲区的大小,以字节为单位,不过它的大小不包含最后一个空字符,如果为值为0,则缓冲区不可用
lpBuffer 缓冲区用来保存所有驱动器的字符串,格式为:"C:\<null>D:\<null>",每个驱动器名称占四个字节,可以通过 lpBuffer + 4 方式依次获取每个驱动器名称
int DsLength = GetLogicalDriveStrings(0,NULL) 如果函数变量为 0 和 NULL ,则用来获取驱动器字符串长度
TCHAR* DStr = new TCHAR[DsLength]
GetLogicalDriveStrings(DsLength, (LPTSTR)DStr)获取所有驱动器字符串信息,信息保存在DStr中
3、获取驱动器类型
UINT GetDriveType(
LPTSTR lpRootPathName //不包含卷名的磁盘根路径名
); 该函数的头文件为"winbase.h"
返回一个无符号int型;类型主要有:
DRIVE_UNKNOWN 未知的磁盘类型
DRIVE_NO_ROOT_DIR 说明 IpRootPathName 是无效的
DRIVE_REMOVABLE 可移动磁盘
DRIVE_FIXED 固定磁盘
DRIVE_REMOTE 网络磁盘
DRIVE_CDROM 光驱
DRIVE_RAMDISK 为RAM
4、获取驱动器磁盘的空间状态
BOOL GetDiskFreeSpaceEx(
LPTSTR lpDirectoryName, //不包含卷名的磁盘根路径名
PULARGE_INTEGER lpFreeBytesAvailable, //当前用户磁盘可用空间
PULARGE_INTEGER lpTotalNumberOfBytes, //磁盘总容量
PULARGE_INTEGER lpTotalNumberOfFreeBytes //磁盘剩余空间
);
实例:
//获取机器所有硬盘信息
#include <iostream>
#include <windows.h>
using namespace std;
void get_diskInfo(CString &message)
{
int DiskCount = 0;
DWORD DiskInfo = GetLogicalDrives();
//利用GetLogicalDrives()函数可以获取系统中逻辑驱动器的数量,函数返回的是一个32位无符号整型数据。
while (DiskInfo)//通过循环操作查看每一位数据是否为1,如果为1则磁盘为真,如果为0则磁盘不存在。
{
if (DiskInfo & 1)//通过位运算的逻辑与操作,判断是否为1
{
++DiskCount;
}
DiskInfo = DiskInfo >> 1;//通过位运算的右移操作保证每循环一次所检查的位置向右移动一位。
}
//cout<<DiskCount<<endl;
int DSLength = GetLogicalDriveStrings(0, NULL);
//通过GetLogicalDriveStrings()函数获取所有驱动器字符串信息长度。
TCHAR* DStr = new TCHAR[DSLength];
GetLogicalDriveStrings(DSLength, (LPTSTR)DStr);
/*for(int i = 0; i< DSLength; i++)
{
cout << DStr[i];
}*/
int DType;
BOOL fResult;
unsigned _int64 i64FreeBytesToCaller;
unsigned _int64 i64TotalBytes;
unsigned _int64 i64FreeBytes;
TCHAR *lpDriveStr = DStr;
char *totalSpace = new char[10];
char *freeSpace = new char[10];
char *percent = new char[10];
message = message + "\"disk\":[";
int first = 0;
for (int i = 0; i< DiskCount; ++i)
{
//GetDriveType函数,可以获取驱动器类型,参数为驱动器的根目录
DType = GetDriveType(lpDriveStr);
//cout<<"name:"<<lpDriveStr<<";DType:"<<DType<<endl;
if (DType == DRIVE_FIXED)
{
fResult = GetDiskFreeSpaceEx(
lpDriveStr,
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);
if (fResult)
{
itoa((i64TotalBytes/ 1024 / 1024 /1024),totalSpace,10);
itoa((i64FreeBytesToCaller/ 1024 / 1024 /1024),freeSpace,10);
int usedpercent = (i64TotalBytes-i64FreeBytesToCaller)*100/i64TotalBytes;
//cout<<usedpercent<<endl;
itoa(usedpercent,percent,10);
CString driver = (char*)lpDriveStr;
//cout<<driver[0]<<endl;
if(first == 0)
{
message = message + "{\"name\":\"" + driver[0] + "\",\"total\":\"" + totalSpace + "GB\",\"unused\":\"" + freeSpace + "GB\",\"percent\":"+ percent +"}";
first = 1;
}
else
{
message = message + ",{\"name\":\"" + driver[0] + "\",\"total\":\"" + totalSpace + "GB\",\"unused\":\"" + freeSpace + "GB\",\"percent\":"+ percent +"}";
}
}
else
{
cout << " 设备未准备好";
}
lpDriveStr += 4;
}
else
{
lpDriveStr += 4;
}
}
message = message + "]";
delete[] totalSpace;
delete[] freeSpace;
delete[] percent;
delete[] DStr;
totalSpace = NULL;
freeSpace = NULL;
percent = NULL;
DStr = NULL;
lpDriveStr = NULL;
return 0;
}
int main()
{
CString message;
get_diskInfo(message);
cout<<message<<endl;
}
头文件为:windows.h
1、获取系统逻辑驱动的数量:
使用 GetLogicalDrives(void) 函数获取逻辑驱动的数量,函数返回一个32无符号整型数据
DWORD diskCount = GetLogicalDrives()
可以通过对返回的无符号整型数据 1 的个数来判断磁盘是否为真,如果为 0 表示磁盘不存在
while(diskCount)
{
if(diskCount & 1)
{
++num;
}
diskCount = diskCount >> 1;
}
num 即为真实存在磁盘的数量
2、获取所有驱动器字符串信息
GetLogicalDriveStrings()函数 获取所有驱动器字符串信息
函数原型为:DWORD GetLogicalDriveStrings(
DWORD nBufferLength,
LPTSTR lpBuffer
);
nBufferLength 表示lpBuffer缓冲区的大小,以字节为单位,不过它的大小不包含最后一个空字符,如果为值为0,则缓冲区不可用
lpBuffer 缓冲区用来保存所有驱动器的字符串,格式为:"C:\<null>D:\<null>",每个驱动器名称占四个字节,可以通过 lpBuffer + 4 方式依次获取每个驱动器名称
int DsLength = GetLogicalDriveStrings(0,NULL) 如果函数变量为 0 和 NULL ,则用来获取驱动器字符串长度
TCHAR* DStr = new TCHAR[DsLength]
GetLogicalDriveStrings(DsLength, (LPTSTR)DStr)获取所有驱动器字符串信息,信息保存在DStr中
3、获取驱动器类型
UINT GetDriveType(
LPTSTR lpRootPathName //不包含卷名的磁盘根路径名
); 该函数的头文件为"winbase.h"
返回一个无符号int型;类型主要有:
DRIVE_UNKNOWN 未知的磁盘类型
DRIVE_NO_ROOT_DIR 说明 IpRootPathName 是无效的
DRIVE_REMOVABLE 可移动磁盘
DRIVE_FIXED 固定磁盘
DRIVE_REMOTE 网络磁盘
DRIVE_CDROM 光驱
DRIVE_RAMDISK 为RAM
4、获取驱动器磁盘的空间状态
BOOL GetDiskFreeSpaceEx(
LPTSTR lpDirectoryName, //不包含卷名的磁盘根路径名
PULARGE_INTEGER lpFreeBytesAvailable, //当前用户磁盘可用空间
PULARGE_INTEGER lpTotalNumberOfBytes, //磁盘总容量
PULARGE_INTEGER lpTotalNumberOfFreeBytes //磁盘剩余空间
);
实例:
//获取机器所有硬盘信息
#include <iostream>
#include <windows.h>
using namespace std;
void get_diskInfo(CString &message)
{
int DiskCount = 0;
DWORD DiskInfo = GetLogicalDrives();
//利用GetLogicalDrives()函数可以获取系统中逻辑驱动器的数量,函数返回的是一个32位无符号整型数据。
while (DiskInfo)//通过循环操作查看每一位数据是否为1,如果为1则磁盘为真,如果为0则磁盘不存在。
{
if (DiskInfo & 1)//通过位运算的逻辑与操作,判断是否为1
{
++DiskCount;
}
DiskInfo = DiskInfo >> 1;//通过位运算的右移操作保证每循环一次所检查的位置向右移动一位。
}
//cout<<DiskCount<<endl;
int DSLength = GetLogicalDriveStrings(0, NULL);
//通过GetLogicalDriveStrings()函数获取所有驱动器字符串信息长度。
TCHAR* DStr = new TCHAR[DSLength];
GetLogicalDriveStrings(DSLength, (LPTSTR)DStr);
/*for(int i = 0; i< DSLength; i++)
{
cout << DStr[i];
}*/
int DType;
BOOL fResult;
unsigned _int64 i64FreeBytesToCaller;
unsigned _int64 i64TotalBytes;
unsigned _int64 i64FreeBytes;
TCHAR *lpDriveStr = DStr;
char *totalSpace = new char[10];
char *freeSpace = new char[10];
char *percent = new char[10];
message = message + "\"disk\":[";
int first = 0;
for (int i = 0; i< DiskCount; ++i)
{
//GetDriveType函数,可以获取驱动器类型,参数为驱动器的根目录
DType = GetDriveType(lpDriveStr);
//cout<<"name:"<<lpDriveStr<<";DType:"<<DType<<endl;
if (DType == DRIVE_FIXED)
{
fResult = GetDiskFreeSpaceEx(
lpDriveStr,
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);
if (fResult)
{
itoa((i64TotalBytes/ 1024 / 1024 /1024),totalSpace,10);
itoa((i64FreeBytesToCaller/ 1024 / 1024 /1024),freeSpace,10);
int usedpercent = (i64TotalBytes-i64FreeBytesToCaller)*100/i64TotalBytes;
//cout<<usedpercent<<endl;
itoa(usedpercent,percent,10);
CString driver = (char*)lpDriveStr;
//cout<<driver[0]<<endl;
if(first == 0)
{
message = message + "{\"name\":\"" + driver[0] + "\",\"total\":\"" + totalSpace + "GB\",\"unused\":\"" + freeSpace + "GB\",\"percent\":"+ percent +"}";
first = 1;
}
else
{
message = message + ",{\"name\":\"" + driver[0] + "\",\"total\":\"" + totalSpace + "GB\",\"unused\":\"" + freeSpace + "GB\",\"percent\":"+ percent +"}";
}
}
else
{
cout << " 设备未准备好";
}
lpDriveStr += 4;
}
else
{
lpDriveStr += 4;
}
}
message = message + "]";
delete[] totalSpace;
delete[] freeSpace;
delete[] percent;
delete[] DStr;
totalSpace = NULL;
freeSpace = NULL;
percent = NULL;
DStr = NULL;
lpDriveStr = NULL;
return 0;
}
int main()
{
CString message;
get_diskInfo(message);
cout<<message<<endl;
}
阅读全文
0 0
- windows 下C++获取磁盘信息笔记
- Windows获取磁盘信息
- C/C++ Windows API——获取磁盘信息
- 在Windows下通过JConfig获取磁盘剩余空间信息
- 在Windows下通过JConfig获取磁盘剩余空间信息
- windows API 获取磁盘信息
- C#-获取磁盘,cpu,内存信息
- Linux下应如何获取磁盘信息
- Linux下使用Sigar 获取磁盘信息
- Java:在Windows下通过JConfig获取磁盘剩余空间信息
- 在Windows下通过JConfig获取磁盘剩余空间信息(续)
- Windows下硬件信息获取
- windows下获取网卡信息
- Windows下USB磁盘开发系列三:枚举系统中U盘、并获取其设备信息
- MFC--获取磁盘信息
- WMI获取磁盘信息
- 获取磁盘信息
- 获取磁盘信息
- CentOS6.5
- K:线性表
- 谈谈ConcurrentHashMap1.7和1.8的不同实现
- 双屏/多显示器截屏
- Chapter 4
- windows 下C++获取磁盘信息笔记
- Qt的QMediaPlayer播放视频和音频
- GOS系列之上传附件
- getOutputStream() has already been called for this response
- WiFiDisplay
- 缓存文件可以放在哪里?它们各自的特点是什么?
- Chapter 4
- uva 11584 计算最少回文串数 动态规划 lrj-P276
- Linux任务前后台的切换