如何创建一个没有Windows分配驱动器盘符的分区? [英] How to create a partition without Windows assigning a drive letter?

查看:428
本文介绍了如何创建一个没有Windows分配驱动器盘符的分区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过Windows API初始化和分区连接的虚拟硬盘。我已成功使用 DeviceIoControl()这样做,但是每当我应用所需的驱动器布局Windows会自动分配一个驱动器号到分区,并弹出一个恼人的您要格式化?对话框。

I am trying to initialize and partition an attached virtual hard disk through the Windows API. I have been successful using DeviceIoControl() to do so, however whenever I apply the desired drive layout Windows is automatically assigning a drive letter to the partition and popping up an annoying "Would you like to format?" dialog.

我的意图是在程序中处理这个分区的格式化和挂载,但我不知道如何停止这个行为。我已经尝试将 RecognizedPartition 设置为FALSE,但这似乎没有效果。

My intent is to handle the formatting and mounting of this partition later in the program, but I'm not sure how to stop this behavior. I have tried setting RecognizedPartition to FALSE, but this seems to have no effect.

相关代码:

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 letter assignment?

推荐答案

我可以找到解决这个问题的唯一可靠的方法是在卷创建和格式化时停止Shell硬件检测服务。

The only reliable way I could find to work around this issue was to stop the "Shell Hardware Detection" service while the volume was created and formatted. However, this approach is so unapologetically silly that I refused to put it into code.

另一个hackish选项是让服务启动,然后立即产生它自己或CREATE_NO_WINDOW 标志

Another "hackish" option is to have the service start up and then immediately spawn itself (or a "worker" executable) in a hidden window via CreateProcess() with the CREATE_NO_WINDOW flag.

由于这个软件作为一个系统服务运行,我不想使代码复杂化只发生一次或两次在系统的生命周期,我只是不得不接受有时在创建分区时偶尔会弹出一个交互式服务检测窗口。

Since this software runs as a system service and I'd rather not complicate the code for something that only happens once or twice over the lifetime of the system, I've just had to accept that sometimes there will occasionally be an Interactive Services Detection window pop up for a few moments while creating the partitions.

如果任何人发现了一个好的方法为了防止格式提示,同时以编程方式创建和格式化驱动器,我会很乐意更改接受的答案(和欠你一杯啤酒)。

If anyone discovers a good method for preventing the format prompt while programmatically creating and formatting a drive, I'll happily change the accepted answer (and owe you a beer).

这篇关于如何创建一个没有Windows分配驱动器盘符的分区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆