c++ - 如何通过 C++ 程序在 Windows Server 2008/2012 上初始化磁盘

标签 c++ windows deviceiocontrol

我们正在尝试通过 C++ 程序使用 Windows 服务器 2008/2012 上某些现有磁盘的属性来初始化磁盘。

我们正在使用 DeviceIoControl() 方法和来自 Disk management control codesIOCTL_DISK_CREATE_DISKIOCTL_DISK_SET_DRIVE_LAYOUT_EX、IOCTL_DISK_SET_PARTITION_INFO_EX 代码使磁盘可供使用。

通过搜索得到如下代码片段

//To open the drive
hDevice = CreateFile( TEXT("\\\\.\\PhysicalDrive7"), 
                      GENERIC_READ | GENERIC_WRITE,       // no access to the drive 
                      FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode 
                      NULL,                               // default security attributes 
                      OPEN_EXISTING,                      // disposition 
                      0,                                  // file attributes 
                      NULL);                              // do not copy file attributes 


CREATE_DISK dsk; 
dsk.PartitionStyle = PARTITION_STYLE_MBR; //It can also be PARTITION_STYLE_GPT
dsk.Mbr.Signature = 1;

// Initialize disk
bResult = DeviceIoControl( hDevice,                 // device to be queried 
                           IOCTL_DISK_CREATE_DISK,  // operation to perform 
                           &dsk, sizeof(dsk),         
                           NULL, 0,                // no output buffer 
                           &junk,                  // # bytes returned 
                           NULL
                         ); 

LARGE_INTEGER lgPartitionSize;
lgPartitionSize.QuadPart = (1024 * 1024 * 1024);
DWORD dwDriverLayoutInfoExLen = sizeof (DRIVE_LAYOUT_INFORMATION_EX) + 3 * sizeof(PARTITION_INFORMATION_EX);
DRIVE_LAYOUT_INFORMATION_EX *pdg = (DRIVE_LAYOUT_INFORMATION_EX *)new BYTE[dwDriverLayoutInfoExLen];

SecureZeroMemory(pdg, dwDriverLayoutInfoExLen);

pdg->PartitionStyle = PARTITION_STYLE_MBR; 
pdg->PartitionCount = 1;
pdg->Mbr.Signature = 1;

pdg->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;   
pdg->PartitionEntry[0].StartingOffset.QuadPart = 1048576;  
pdg->PartitionEntry[0].PartitionLength.QuadPart = lgPartitionSize.QuadPart * 200;   
pdg->PartitionEntry[0].PartitionNumber = 1;   
pdg->PartitionEntry[0].RewritePartition = TRUE;

pdg->PartitionEntry[0].Mbr.PartitionType = PARTITION_NTFT; // PARTITION_IFS (NTFS partition or logical drive)   
pdg->PartitionEntry[0].Mbr.BootIndicator = TRUE;
pdg->PartitionEntry[0].Mbr.RecognizedPartition = 1;   
pdg->PartitionEntry[0].Mbr.HiddenSectors = 32256 / 512;   

// Partition a disk
bResult = DeviceIoControl( hDevice,        // device to be queried 
                           IOCTL_DISK_SET_DRIVE_LAYOUT_EX,  // operation to perform 
                           pdg, sizeof DRIVE_LAYOUT_INFORMATION_EX, //output buffer
                           NULL, 0,                // no output buffer 
                           &junk,                    // # bytes returned 
                           NULL
                         ); 

bResult = DeviceIoControl(  hDevice, 
                            IOCTL_DISK_UPDATE_PROPERTIES, 
                            NULL, 0, NULL, 0, &junk, NULL);

PARTITION_INFORMATION_EX dskinfo; 
PARTITION_INFORMATION_MBR mbrinfo; 
mbrinfo.PartitionType = PARTITION_NTFT; 
mbrinfo.HiddenSectors = (32256 / 512);
mbrinfo.BootIndicator = 1;
mbrinfo.RecognizedPartition = 1;

dskinfo.PartitionStyle = PARTITION_STYLE_MBR; 
dskinfo.StartingOffset.QuadPart = 1048576;//0; 
dskinfo.PartitionLength.QuadPart = lgPartitionSize.QuadPart * 200; 
dskinfo.PartitionNumber = 1; 
dskinfo.RewritePartition = TRUE; 
dskinfo.Mbr = mbrinfo;


bResult = DeviceIoControl( hDevice,        // device to be queried 
                           IOCTL_DISK_SET_PARTITION_INFO_EX,  // operation to perform 
                           &dskinfo, sizeof(dskinfo),        // output buffer 
                           NULL, 0,                // no output buffer 
                           &junk,                    // # bytes returned   
                           NULL
                         ); 

DeviceIoControl() 的所有调用都成功了,除了最后一个带有错误 1 ​​的 IOCTL_DISK_SET_PARTITION_INFO_EX 代码(即函数不正确)。这可能是什么原因?

如果我们注释掉最后一个调用,磁盘被初始化为原始磁盘,但这不符合我们的要求。

以上示例仅适用于 MBR 分区样式。我们找不到 GPT 的任何示例,...样式。如果有人知道,请提供链接。

最佳答案

您在 IOCTL_DISK_SET_PARTITION_INFO_EX 中使用了错误的结构类型。它采用 SET_PARTITION_INFORMATION_EX 结构,而不是 PARTITION_INFORMATION_EX 结构。

您可能不需要使用 IOCTL_DISK_SET_PARTITION_INFO_EX,因为它只是设置分区类型,这应该已经用 IOCTL_DISK_SET_DRIVE_LAYOUT_EX 设置了。不幸的是你用它来设置错误的分区类型。 NTFS 分区的分区类型为 PARTITION_IFS

lgPartitionSize 乘以 200 几乎肯定是错误的。如果 lgPartitionSize 假定为扇区大小,则需要将其乘以磁盘的扇区大小。硬盘驱动器的扇区大小过去始终为 512 字节(0x200 字节),但现代驱动器使用 4096 字节的扇区大小。

正确创建分区表并不容易,像你这样盲目复制别人的代码是行不通的。即使在您解决了我上面提到的问题之后,您仍然可能会遇到其他问题。您确实需要了解对分区布局的所有限制。

您可能需要考虑使用 diskpart命令以编程方式初始化磁盘而不是 C++ 代码。

关于c++ - 如何通过 C++ 程序在 Windows Server 2008/2012 上初始化磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25792646/

相关文章:

C++ 模板,默认参数作为方法

c++ - 如何使用 BOOST_FOREACH 同时迭代两个 vector ?

windows - 如果由任务计划程序运行,则无法访问网络地址

windows - 如何在 Windows 上使用类 Linux Shell (bash) 进行开发?

c# - 使用 C# 在 USB 上创建多个分区

c++ - 多项式最大公约数 C++

c++ - 输入特征样式来测试 T 是否是 basic_string<> 或可打印的类似字符串类型?

c++ - "Dual-target"MinGW-w64 不是真的双目标?

c++ - 带有 IOCTL_DISK_GET_DRIVE_GEOMETRY 的 DeviceIoControl() 失败并返回错误代码 87。为什么?

c - 使用 DeviceIoControl 从应用程序向驱动程序发送数据