>I use DeviceIoControl to create four primary NTFS partitions on a HardDisk.
>Partitions are created properly if I use a new Hard Disk, but if I use an
>HardDisk already formatted and erased,
>often drive letters are assigned in revers order.
>>I use DeviceIoControl to create four primary NTFS partitions on a
>>HardDisk.
[quoted text clipped - 22 lines]
> Omega Techware
> http://www.omegatechware.net
Yes, It do not happen all the time, some time it assign letters in right
order.
the code I use to create partitions on disk is:
This is the code I use to crate partitions.
char str[128];
wsprintf(str,"\\\\.\\PhysicalDrive%d", i);
hDevice = CreateFile( ,"\\\\.\\PhysicalDrive1, // drive to open
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL );
LARGE_INTEGER dimDiscoD;
LARGE_INTEGER dimDiscoE;
LARGE_INTEGER dimDiscoF;
LARGE_INTEGER dimDiscoG;
DISK_GEOMETRY_EX pDiskGeometryEx;
DWORD dwBytesReturned;
BOOL r = DeviceIoControl(
hDevice,
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
NULL,
0,
&pDiskGeometryEx,
sizeof(pDiskGeometryEx),
&dwBytesReturned,
NULL
);
dimDiscoG.QuadPart = DIM_DISCOG;
dimDiscoD.QuadPart = (( pDiskGeometryEx.DiskSize.QuadPart -
dimDiscoG.QuadPart )/6) * 4;
dimDiscoE.QuadPart = dimDiscoD.QuadPart/4;
dimDiscoF.QuadPart = dimDiscoE.QuadPart;
CREATE_DISK pCreateDisk;
ZeroMemory(&pCreateDisk,sizeof(CREATE_DISK));
DWORD dwBytesReturned;
pCreateDisk.PartitionStyle = PARTITION_STYLE_MBR;
pCreateDisk.Mbr.Signature = 0xA4B57310;
DeviceIoControl(
hDevice,
IOCTL_DISK_CREATE_DISK, // operation to perform
(LPVOID)&pCreateDisk,
sizeof(pCreateDisk),
NULL,
0,
&dwBytesReturned,
(LPOVERLAPPED) NULL)
DWORD junk1;
DeviceIoControl(
hDevice,
IOCTL_DISK_UPDATE_PROPERTIES,
NULL,
0,
NULL,
0,
&junk1,
NULL
);
int SectorSize=pDiskGeometryEx.Geometry.BytesPerSector;
DWORD szNewLayout = sizeof(DRIVE_LAYOUT_INFORMATION_EX)+4*sizeof
PARTITION_INFORMATION_EX);
DRIVE_LAYOUT_INFORMATION_EX *dl = (DRIVE_LAYOUT_INFORMATION_EX*) new
BYTE[szNewLayout];
ZeroMemory(dl,szNewLayout);
dl->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;
dl->PartitionEntry[0].StartingOffset.QuadPart = 32256i64; // = 63 *
512
dl->PartitionEntry[0].PartitionLength.QuadPart = dimDiscoD.QuadPart;
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= 32256i64 +
dimDiscoD.QuadPart;
dl->PartitionEntry[1].PartitionLength.QuadPart = dimDiscoE.QuadPart;
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 +
dimDiscoD.QuadPart)/SectorSize; //SectorSize;
dl->PartitionEntry[2].PartitionStyle=PARTITION_STYLE_MBR;
dl->PartitionEntry[2].StartingOffset.QuadPart= 32256i64 +
dimDiscoD.QuadPart + dimDiscoE.QuadPart;
dl->PartitionEntry[2].PartitionLength.QuadPart = dimDiscoF.QuadPart;
dl->PartitionEntry[2].PartitionNumber=3;
dl->PartitionEntry[2].RewritePartition = TRUE;
dl->PartitionEntry[2].Mbr.PartitionType = 0x07;
dl->PartitionEntry[2].Mbr.RecognizedPartition = 1;
dl->PartitionEntry[2].Mbr.HiddenSectors = (32256i64 +
dimDiscoD.QuadPart + dimDiscoE.QuadPart)/SectorSize; //SectorSize;
dl->PartitionEntry[3].PartitionStyle=PARTITION_STYLE_MBR;
dl->PartitionEntry[3].StartingOffset.QuadPart= 32256i64 +
dimDiscoD.QuadPart + dimDiscoE.QuadPart + dimDiscoF.QuadPart;
dl->PartitionEntry[3].PartitionLength.QuadPart =
pDiskGeometryEx.DiskSize.QuadPart -
(dimDiscoD.QuadPart + dimDiscoE.QuadPart + dimDiscoF.QuadPart );
dl->PartitionEntry[3].PartitionNumber=4;
dl->PartitionEntry[3].RewritePartition = TRUE;
dl->PartitionEntry[3].Mbr.PartitionType = 0x07;
dl->PartitionEntry[3].Mbr.RecognizedPartition = 1;
dl->PartitionEntry[3].Mbr.HiddenSectors = (32256i64 +
dimDiscoD.QuadPart + dimDiscoE.QuadPart + dimDiscoF.QuadPart )/SectorSize;
//SectorSize;
dl->PartitionStyle = PARTITION_STYLE_MBR;
dl->PartitionCount = 4;// specify AT LEAST 4 partitions!!!
dl->Mbr.Signature = 0xA4B57310; //
DeviceIoControl(
hDevice, // device we are querying
IOCTL_DISK_SET_DRIVE_LAYOUT_EX, // operation to perform
dl,
szNewLayout,
NULL,
0, // no input buffer, so pass zero
&junk, // discard count of bytes returned
(LPOVERLAPPED) NULL) // synchronous I/O
)
// update disk properties
DWORD junk;
DeviceIoControl(
hDevice,
IOCTL_DISK_UPDATE_PROPERTIES,
NULL,
0,
NULL,
0,
&junk,
NULL
);
CloseHandle(hDevice);
delete dl;
Regards,
Gianluca
Jeremiah D. Seitz - 27 Sep 2008 00:01 GMT
>Yes, It do not happen all the time, some time it assign letters in right
>order.
That *is* puzzling ...
>This is the code I use to crate partitions.
8< SNIP >8
I'd be remiss if I didn't point out that this is a VB newsgroup. You
might have better luck in a C group.
In the meantime, have you tried setting RecognizedPartition to 0? The
MSDN docs aren't very clear on the actual effect of this member.
Perhaps setting it to 0 would prevent the volumes from being
automatically mounted? If so, then you could possibly take control of
the actual drive letter assignments.
Offhand, I wouldn't know where to look for confirmation, but ...
TrueCrypt performs both volume creation and drive letter assignment,
and it's open source. Being written in C, it should be right up your
alley.
Out of curiosity, how did you arrive at your MBR signature?
>Regards,
>Gianluca
Jeremiah D. Seitz
Omega Techware
http://www.omegatechware.net
Gianluca - 29 Sep 2008 08:15 GMT
>>Yes, It do not happen all the time, some time it assign letters in right
>>order.
[quoted text clipped - 28 lines]
> Omega Techware
> http://www.omegatechware.net
Thank You for your answer.
I took signature from a code I found on Internet.
I used that number as I read somewhere that it can be a random number.