人气 157

[游戏程序] C++ 通过WIN32 API 获取逻辑磁盘详细信息 [复制链接]

九艺网 2017-3-10 17:00:23

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
[table][tr][td]
下载源文件

今天我们主要介绍的是几个常用的api函数,通过它我们可以获取用户磁盘的相关信息。


其主要函数原型说明如下:


1.获取系统中逻辑驱动器的数量

The GetLogicalDrives function retrieves a bitmask representing the currently available disk drives.
DWORD GetLogicalDrives(void);

2.获取所有驱动器字符串信息

The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system.

DWORD GetLogicalDriveStrings(
DWORD nBufferLength,

LPTSTR lpBuffer
);
3.获取驱动器类型

The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.

UINT GetDriveType(
LPCTSTR lpRootPathName
);
4. 获取驱动器磁盘的空间状态,函数返回的是个BOOL类型数据

The GetDiskFreeSpaceEx function retrieves information about the amount of space available on a disk volume: the total amount of space, the total amount of free space, and the total amount of free space available to the user associated with the calling thread.

BOOL GetDiskFreeSpaceEx(
LPCTSTR lpDirectoryName,

PULARGE_INTEGER lpFreeBytesAvailable,

PULARGE_INTEGER lpTotalNumberOfBytes,

PULARGE_INTEGER lpTotalNumberOfFreeBytes
);
以下是完整的示例程序代码

//程序作者:管宁

//站点:www.cndev-lab.com     
//所有稿件均有版权,如要转载,请务必注名出处和作者

#include

#include

using namespace std;


int main()

{


int DiskCount = 0;


DWORD DiskInfo = GetLogicalDrives();


//
利用GetLogicalDrives()函数可以获取系统中逻辑驱动器的数量,函数返回的是一个32位无符号整型数据。


while(DiskInfo)//
通过循环操作查看每一位数据是否为1,如果为1则磁盘为真,如果为0则磁盘不存在。


{


if(DiskInfo&1)//
通过位运算的逻辑与操作,判断是否为1


{


++DiskCount;


}


DiskInfo = DiskInfo >> 1;//
通过位运算的右移操作保证每循环一次所检查的位置向右移动一位。


//DiskInfo = DiskInfo/2;


}

<div align="left"><font face="NSimSun"><font style="font-size:9pt">
<font color="navy">cout
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

QQ|手机版|小黑屋|九艺游戏动画论坛 ( 津ICP备2022000452号-1 )

GMT+8, 2024-5-13 19:40 , Processed in 0.057022 second(s), 23 queries .

Powered by Discuz! X3.4  © 2001-2017 Discuz Team.