• 大小: 1004KB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2023-08-28
  • 语言: C/C++
  • 标签: c++  硬盘信息  harddisk  

资源简介

本项目采用vs2010开发,目的是获取本地硬盘的个数,硬盘容量,硬盘上的分区, 以及分区的大小和名称

资源截图

代码片段和文件信息

// MY_Get_Disk_Info.cpp : 定义控制台应用程序的入口点。
/************************************************************************/
/*   本项目用来获取本机上本地硬盘个数,以及容量(名称可以枚举设备树获得),
 每一块硬盘上的分区,以及分区名称及容量。
 当时将硬盘和分区对应时着实花了一些时间。
---by za
/************************************************************************/


#include “stdafx.h“
#include 


BOOL IsPartitionOfDisk(int nDiskNum char* strDrive)
{
char cstrDrive[20] = {0};
HANDLE hPartition = NULL;
STORAGE_DEVICE_NUMBER storage_device_number;
DWORD dwBytesReturned = 0;
BOOL blErr = FALSE;
BOOL blPartitionOfDisk = FALSE;
char strDriveTemp[3] = {0};
strncpy(strDriveTemp strDrive 2);
sprintf(cstrDrive “\\\\.\\%s“ strDriveTemp);

hPartition = ::CreateFile(
cstrDrive
GENERIC_READ | GENERIC_WRITE
FILE_SHARE_READ | FILE_SHARE_WRITE
NULLOPEN_EXISTING
FILE_ATTRIBUTE_NORMAL
NULL
);

if (INVALID_HANDLE_VALUE == hPartition)
{
//打印dbglog日志 打开卷失败
goto __exit;
}

blErr = ::DeviceIoControl(
hPartition
IOCTL_STORAGE_GET_DEVICE_NUMBER
NULL
0
&storage_device_number
sizeof(storage_device_number)
&dwBytesReturned
(LPOVERLAPPED)NULL
);

if (FALSE == blErr || storage_device_number.PartitionNumber == -1)
{
//打印dbglog 获取devicenumer失败
goto __exit;
}
if (nDiskNum == storage_device_number.DeviceNumber)
{
blPartitionOfDisk = TRUE;
}
__exit:
return blPartitionOfDisk;
}


DWORD GetPartitionSize(char* DirectoryName)
{
ULARGE_INTEGER ulFreeBytesAvailable = {0};
ULARGE_INTEGER ulTotalNumberOfBytes = {0};
ULARGE_INTEGER ulTotalNumberOfFreeBytes = {0};
BOOL blErr = FALSE;
DWORD dwPartitionSize = 0;

blErr = GetDiskFreeSpaceEx(DirectoryName &ulFreeBytesAvailable &ulTotalNumberOfBytes &ulTotalNumberOfFreeBytes);

if (FALSE == blErr)
{
//打印dbglog日志 获取分区大小失败
goto __exit;
}

dwPartitionSize = ulTotalNumberOfBytes.QuadPart/(1024*1024*1024);
__exit:
return dwPartitionSize;
}

void GetPartitionsInfoOfDisk(int nDiskNum char* strDrives)
{
char strDriveTem[4] = {0};
DWORD dwPartitionSize[10] = {0};
int nPartitionCount = 0;
BOOL blErr = FALSE;

strcpy(strDriveTem strDrives);
while (NULL != *strDrives)
{
blErr = IsPartitionOfDisk(nDiskNum strDriveTem);
if (TRUE == blErr)
{
dwPartitionSize[nPartitionCount] = GetPartitionSize(strDriveTem);
printf(“PartitionName:%s PartitionSize: %d\n“ strDriveTem dwPartitionSize[nPartitionCount]);
nPartitionCount++;
}
strDrives += 4;
strcpy(strDriveTem strDrives);
}
return;
}

BOOL GetAllLogicalPartitions(char* strDrives int nSize)
{
BOOL blErr = FALSE;
if (0 == ::GetLogicalDriveStrings( nSize strDrives) )
{
//打印dbglog日志 获得逻辑驱动

评论

共有 条评论