BOOL Result; // used to read bad DeviceIoControl calls
DWORD szReturned;
unsigned int SectorSize = 512;
LARGE_INTEGER DiskSize.QuadPart = 40007761920i64;
LARGE_INTEGER Part_1_size.QuadPart = 27406600704i64;
LARGE_INTEGER Part_2_size.QuadPart =40007761920i64-27406600704i64;
// Very important! Size correctly this structure. Even if there's only
// one primary partition, you MUST size the buffer to contain
// AT LEAST 4 PARTITION_INFORMATION_EX!
DWORD szNewLayout = sizeof(DRIVE_LAYOUT_INFORMATION_EX)+4*sizeof(PARTITION_INFOR MATION_EX);
DRIVE_LAYOUT_INFORMATION_EX *dl = (DRIVE_LAYOUT_INFORMATION_EX*) new BYTE[sznewlayout];
// Open handle to physical device
// NtCreateFile() function can be used too with "\\device\\harddisk1\\partiton0" path.
hDrive=CreateFile("\\\\.\\PhysicalDrive1",GENERIC_READ|GEN ERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, //default security attributes
OPEN_EXISTING, // disposition
0,// file attributes
NULL);
if(!hDrive){
// handle the error
}
CREATE_DISK disk;
ZeroMemory(&disk,sizeof(CREATE_DISK));
disk.PartitionStyle = PARTITION_STYLE_MBR;
disk.Mbr.Signature = 0xA4B57300;// the signature can be randomly generated
// Create primary partition MBR
Result = DeviceIoControl(hDrive,IOCTL_DISK_CREATE_DISK,&disk,size of(CREATE_DISK),NULL,0,&szReturned,NULL);
if(!Result){
// handle the error
}
DeviceIoControl(hDrive,IOCTL_DISK_UPDATE_PROPERTIES,
NULL,0,NULL,0,&szReturned,NULL);
//Setup drive layout
ZeroMemory(dl,szNewLayout);
dl->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;
dl->PartitionEntry[0].StartingOffset.QuadPart = 32256;
dl->PartitionEntry[0].PartitionLength = Part_1_Size;
dl->PartitionEntry[0].PartitionNumber = 1;
dl->PartitionEntry[0].RewritePartition = TRUE;
dl->PartitionEntry[0].Mbr.PartitionType = 0x07;// PARTITION_IFS (NTFS partition or logical drive)
dl->PartitionEntry[0].Mbr.BootIndicator = TRUE;
dl->PartitionEntry[0].Mbr.RecognizedPartition = 1;
dl->PartitionEntry[0].Mbr.HiddenSectors=32256/SectorSize;
dl->PartitionEntry[1].PartitionStyle=PARTITION_STYLE_MBR;
dl->PartitionEntry[1].StartingOffset.QuadPart= Part_1_Size.QuadPart + 32256i64;
dl->PartitionEntry[1].PartitionLength = Part_2_Size;
dl->PartitionEntry[1].PartitionNumber=2;
dl->PartitionEntry[1].RewritePartition = TRUE;
dl->PartitionEntry[1].Mbr.PartitionType = 0x07;
dl->PartitionEntry[1].Mbr.RecognizedPartition = 1;
dl->PartitionEntry[1].Mbr.HiddenSectors = (32256i64+Part_1_Size.QuadPart)/SectorSize;
// set RewritePartition=true in every partition to force rewrite.
for (int item=0;item<4;item++)
dl->PartitionEntry[item].RewritePartition = 1;
// setup drive layout
dl->PartitionStyle = PARTITION_STYLE_MBR;
dl->PartitionCount = 4;// specify AT LEAST 4 partitions!!!
dl->Mbr.Signature = 0xA4B57300;
// Set layout
Result = DeviceIoControl(hDrive,IOCTL_DISK_SET_DRIVE_LAYOUT_EX,
& ; ;nbs p; dl,szNewLayout,NULL,0,& ; ; ;szReturned,NULL);
if(!Result)
throw Exception(WhatError());
// update disk properties
DeviceIoControl(hDrive,IOCTL_DISK_UPDATE_PROPERTIES,
NULL,0,NULL,0,&szReturned,NULL);
CloseHandle(hDrive);
delete dl;