用DeviceIoControl获取系统磁盘信息
来源:互联网 发布:网民网络素养教育工作 编辑:程序博客网 时间:2024/05/17 23:21
原文地址:http://www.cppblog.com/dingding/archive/2008/09/23/62570.html
代码:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
SECURITY_ATTRIBUTES secStru;
secStru.bInheritHandle=0;
secStru.lpSecurityDescriptor=0;
secStru.nLength=0;
HANDLE hDevice=CreateFile("\\\\.\\PhysicalDrive0",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
if(hDevice==INVALID_HANDLE_VALUE)
{
return -1;
}
//DWORD outBuff[1000];
GET_LENGTH_INFORMATION infoStruct;
memset(&infoStruct,0,sizeof(infoStruct));
DWORD bytesReturned;
if(DeviceIoControl(hDevice,IOCTL_DISK_GET_LENGTH_INFO,NULL,0,&infoStruct,sizeof(infoStruct),&bytesReturned,NULL)==0)
{
cout<<"Failed to get disk information."<<endl;
DWORD error;
error=GetLastError();
HRESULT hRe=HRESULT_FROM_WIN32(error);
char errorData[10];
sprintf(errorData,"%x",hRe);
cout<<"Error code:"<</*hRe*/errorData<<endl;
CloseHandle(hDevice);
return -1;
}
DISK_GEOMETRY_EX geoStruct;
memset(&geoStruct,0,sizeof(geoStruct));
if(DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY_EX ,NULL,0,&geoStruct,sizeof(geoStruct),&bytesReturned,NULL)==0)
{
cout<<"Failed to get disk information."<<endl;
DWORD error;
error=GetLastError();
HRESULT hRe=HRESULT_FROM_WIN32(error);
char errorData[10];
sprintf(errorData,"%x",hRe);
cout<<"Error code:"<</*hRe*/errorData<<endl;
CloseHandle(hDevice);
return -1;
}
cout<<"The disk's size is:"<<infoStruct.Length.QuadPart/1024/1024/1024<<" G Bytes."<<endl;
cout<<"The disk's cylinder number:"<<geoStruct.Geometry.Cylinders.QuadPart<<endl;
cout<<"The disk's media type:"<<geoStruct.Geometry.MediaType<<endl;
cout<<"Number of tracks per cylinder:"<<geoStruct.Geometry.TracksPerCylinder<<endl;
cout<<"Number of sectors per track:"<<geoStruct.Geometry.SectorsPerTrack<<endl;
cout<<"Number of bytes per sector:"<<geoStruct.Geometry.BytesPerSector<<endl;
PDISK_PARTITION_INFO partitionInfo=DiskGeometryGetPartition(&geoStruct);
DRIVE_LAYOUT_INFORMATION_EX layOutInfo[20];
memset(&layOutInfo,0,sizeof(DRIVE_LAYOUT_INFORMATION_EX)*20);
//layOutInfo.PartitionEntry=*(new PARTITION_INFORMATION_EX[10]);
if(DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_LAYOUT_EX,NULL,0,&layOutInfo,sizeof(DRIVE_LAYOUT_INFORMATION_EX)*20,&bytesReturned,NULL)==0)
{
cout<<"Failed to get disk information."<<endl;
DWORD error;
error=GetLastError();
HRESULT hRe=HRESULT_FROM_WIN32(error);
char errorData[10];
sprintf(errorData,"%x",hRe);
cout<<"Error code:"<</*hRe*/errorData<<endl;
CloseHandle(hDevice);
return -1;
}
int partitionCount=layOutInfo[0].PartitionCount;
cout<<"Number of partitions:"<<layOutInfo[0].PartitionCount<<endl;
cout<<"Partitions' information:"<<endl;
for(int i=0;i<partitionCount;i++)
{
//PDISK_PARTITION_INFO pParInfo=partitionInfo+i*sizeof(DISK_PARTITION_INFO);
if(layOutInfo[i].PartitionEntry[0].PartitionNumber!=0)
{
cout<<"Partition "<<layOutInfo[i].PartitionEntry[0].PartitionNumber<<", partition size is "<<layOutInfo[i].PartitionEntry[0].PartitionLength.QuadPart/1024/1024/1024<<" G Bytes, partition style is "<<layOutInfo[i].PartitionEntry[0].PartitionStyle<<endl;
}
}
//cout<<"The type of partition:"<<((partitionInfo.PartitionStyle==PARTITION_STYLE_MBR) ?"MBR":((partitionInfo.PartitionStyle==PARTITION_STYLE_GPT )?"GPT":((partitionInfo.PartitionStyle==PARTITION_STYLE_RAW)?"RAW":"")))<<endl;
CloseHandle(hDevice);
return 0;}
- 用DeviceIoControl获取系统磁盘信息
- GetVolumeInformation获取磁盘信息与DeviceIoControl获取硬盘信息
- 获取系统磁盘、CPU、内存信息
- 用vbs脚本获取磁盘信息
- 用WMI获取磁盘硬件信息
- MFC--获取磁盘信息
- WMI获取磁盘信息
- 获取磁盘信息
- 获取磁盘信息
- 获取磁盘容量信息
- linux获取磁盘信息
- Sigar获取磁盘信息
- SDK获取磁盘信息
- 获取磁盘符信息
- c#获取磁盘信息
- c++获取磁盘信息
- MFC获取磁盘信息
- Windows获取磁盘信息
- Collectoin接口与Map接口的区别
- Java并发容器之阻塞队列简介
- 前端js性能测试工具(JSLitmus)的使用
- LeetCode | 46. Permutations
- android 以太网和wifi共存
- 用DeviceIoControl获取系统磁盘信息
- HDU 2966 KDtree模板
- php 使用 domxpath读写元素
- Spark SQL之queryExecution运行流程解析Logical Plan(三)
- 小M开发_MySql零基础_day170628
- 数据结构和算法
- php_screw 源码加密
- 时间服务器客户端
- JAVA获取计算机的IP、名称、操作系统等信息