How to create a partition without Windows by assigning a drive letter?

I am trying to initialize and split an attached virtual hard drive through the Windows API. I have successfully used DeviceIoControl () , but whenever I use the desired drive format, Windows automatically assigns the drive letter to the partition and causes the annoying "Want to format?" dialog window.

I intend to handle the formatting and installation of this section later in the program, but I'm not sure how to stop this behavior. I tried setting RecognizedPartition to FALSE, but this does not seem to have an effect.

Relevant Code:

 Layout.PartitionStyle = PARTITION_STYLE_MBR; Layout.PartitionCount = 4; Layout.Mbr.Signature = MY_DISK_MBR_SIGNATURE; Layout.PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR; Layout.PartitionEntry[0].PartitionNumber = 1; Layout.PartitionEntry[0].StartingOffset.QuadPart = MY_DISK_OFFSET; Layout.PartitionEntry[0].PartitionLength.QuadPart = (Geom.DiskSize.QuadPart - MY_DISK_OFFSET); Layout.PartitionEntry[0].Mbr.PartitionType = PARTITION_IFS; Layout.PartitionEntry[0].Mbr.BootIndicator = FALSE; Layout.PartitionEntry[0].Mbr.RecognizedPartition = FALSE; Layout.PartitionEntry[0].Mbr.HiddenSectors = (MY_DISK_OFFSET / Geom.Geometry.BytesPerSector); for (int i = 0; i < 4; i++) { Layout.PartitionEntry[i].RewritePartition = TRUE; } if (!DeviceIoControl(hDisk, IOCTL_DISK_SET_DRIVE_LAYOUT_EX, Layout, dwLayoutSz, NULL, 0, &dwReturn, NULL)) { // Handle error } DeviceIoControl(hDisk, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwReturn, NULL); 

What can I do to prevent automatic drive lettering?

+8
c ++ windows-7 winapi
source share
4 answers

The only reliable way I could find a solution to this problem was to terminate the "Hardware Discovery" service when the volume was created and formatted. However, this approach is so neapologically stupid that I refused to include it in the code.

Another β€œhacker” option is to start the service and then immediately launch itself (or the β€œworking” executable file) in a hidden window via CreateProcess() with the CREATE_NO_WINDOW flag.

Since this software works as a system service, and I would not complicate the code for something that happens only once or twice during the life of the system, I just had to accept that sometimes there will be an interactive Service Discovery window appears a few seconds when creating partitions.

If someone finds a good way to prevent format prompts when creating and formatting a disk programmatically, I will gladly change the accepted answer (and I owe you a beer).

+2
source share

It has been a while since I used this API, but from memory you cannot. But this does not stop you from removing the drive letter assignment after the fact.

I'm not sure if it will stop the tho format prompt, all the time I did, this section is already formatted correctly before I update the disk layout.

0
source share

I just solved this problem, waiting a few seconds for the disk to be accessible, and then directly outputting the format action. See my answer here .

0
source share

Rufus has an interesting workaround: it hooks an event window hook that determines "do you want to format this drive?". requests and closes them immediately. See the source code here .

Then he organizes the installation of only partitions that he needs, but which are orthogonal.

0
source share

All Articles