得到硬盘分区信息
// GetPartitionInfo.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#pragma pack(1)
typedef struct _PARTITION_ENTRY
{
UCHAR BootIndicator; // 能否启动标志
UCHAR StartHead; // 该分区起始磁头号
UCHAR StartSector; // 起始柱面号高2位:6位起始扇区号
UCHAR StartCylinder; // 起始柱面号低8位
UCHAR PartitionType; // 分区类型
UCHAR EndHead; // 该分区终止磁头号
UCHAR EndSector; // 终止柱面号高2位:6位终止扇区号
UCHAR EndCylinder; // 终止柱面号低8位
ULONG StartLBA; // 起始扇区号
ULONG TotalSector; // 分区尺寸(总扇区数)
}PARTITION_ENTRY,*PPARTITION_ENTRY;
typedef struct _MBR_SECTOR
{
UCHAR BootCode[446];
PARTITION_ENTRY Partition[4];
USHORT Signature;
}MBR_SECTOR,*PMBR_SECTOR;
#pragma pack()
void DumpPartitionInfo(PPARTITION_ENTRY Partition)
{
printf("\nInformation: \nBootIndicator: 0x%X.\nStartHead: 0x%X.\nStartSector: 0x%X.\nStartCylinder: 0x%X.\nPartitionType: 0x%X.\nEndHead: 0x%X.\nEndSector: 0x%X.\nEndCylinder: 0x%X.\nStartLBA: 0x%X.\nTotalSector: 0x%X.\n",
Partition->BootIndicator,
Partition->StartHead,
Partition->StartSector,
Partition->StartCylinder,
Partition->PartitionType,
Partition->EndHead,
Partition->EndSector,
Partition->EndCylinder,
Partition->StartLBA,
Partition->TotalSector);
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hPhysicalDrive0;
MBR_SECTOR MbrSector;
DWORD BytesRead;
LONG HighPart=0;
hPhysicalDrive0=::CreateFile(L"\\\\.\\PhysicalDrive0",GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,NULL,OPEN_EXISTING,0,0);
if (hPhysicalDrive0==INVALID_HANDLE_VALUE)
{
printf("Error: Cannot open \\\\.\\PhysicalDrive0.\n");
}
else
{
printf("Information: Opening \\\\.\\PhysicalDrive0 succeeded.\n");
SetFilePointer(hPhysicalDrive0,0,0,FILE_BEGIN);
}
if(ReadFile(hPhysicalDrive0,&MbrSector,sizeof(MBR_SECTOR),&BytesRead,NULL))
{
DWORD PartitionIndex=0;
printf("Information: Signature: 0x%X.\n",MbrSector.Signature);
for (PartitionIndex=0;PartitionIndex<4;PartitionIndex++)
{
printf("\nInformation: Parition %d: \n",PartitionIndex);
DumpPartitionInfo(&MbrSector.Partition[PartitionIndex]);
if (MbrSector.Partition[PartitionIndex].PartitionType==0xF)
{
// 扩展分区
MBR_SECTOR EbrSector;
LARGE_INTEGER Position;
Position.QuadPart=0;
Position.QuadPart+=(LONGLONG)MbrSector.Partition[PartitionIndex].StartLBA*(LONGLONG)512;
do
{
SetFilePointer(hPhysicalDrive0,Position.LowPart,&Position.HighPart,FILE_BEGIN);
RtlZeroMemory(&EbrSector,sizeof(MBR_SECTOR));
if(ReadFile(hPhysicalDrive0,&EbrSector,sizeof(MBR_SECTOR),&BytesRead,NULL))
{
DumpPartitionInfo(&EbrSector.Partition[0]);
Position.QuadPart=((LONGLONG)MbrSector.Partition[PartitionIndex].StartLBA+(LONGLONG)EbrSector.Partition[1].StartLBA)*(LONGLONG)512;
}
else
{
printf("Error: ReadFile failed while tring to read Ebr.\n");
break;
}
}while (EbrSector.Partition[1].StartSector);
}
}
}
CloseHandle(hPhysicalDrive0);
printf("Information: Exited.\n");
return 0;
} 快来参与哦
页:
[1]