编程实现windows中磁盘初始化、磁盘分区格式化功能遇到的有关问题
编程实现windows中磁盘初始化、磁盘分区格式化功能遇到的问题?
当PC中新增加硬盘或者添加了一个虚拟的磁盘设备时,通过windows的磁盘管理工具可以完成磁盘的初始化、磁盘分区格式化。现在希望在只知道磁盘设备(例如,\\\\.\\PhysicalDrive0)的前提下完成像windows磁盘管理工具一样的功能。现在通过DeviceIoControl API发送磁盘相关指令,但是代码存在以下问题:磁盘可以初始化,可以完成分区,但是磁盘初始化分区后的磁盘引导扇区数据不对。并且这部分代码没有格式化分区功能,代码如下:
int InitDisk(void)
{
BOOL bResult = FALSE; // generic results flag
CAutoFile hDevice; // handle to the drive to be examined
DWORD junk = 0; // discard results
hDevice = CreateFile(TEXT("\\\\.\\PhysicalDrive0"), //"\\\\.\\PhysicalDrive1", // drive to open
GENERIC_READ | GENERIC_WRITE, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return GetLastError();
}
CREATE_DISK dsk;
dsk.PartitionStyle = PARTITION_STYLE_MBR;
dsk.Mbr.Signature = 9999;
// 初始化磁盘
bResult = DeviceIoControl(hDevice, // device to be queried
IOCTL_DISK_CREATE_DISK, // operation to perform
&dsk, sizeof(dsk), //sizeof(pdg), // output buffer
NULL, 0, // no output buffer
&junk, // # bytes returned
NULL);
if (!bResult)
{
return GetLastError();
}
bResult = DeviceIoControl(hDevice,
IOCTL_DISK_UPDATE_PROPERTIES,
NULL, 0, NULL, 0, &junk, NULL);
if (! bResult)
{
return GetLastError();
}
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];
if (pdg == NULL)
{
return -1;
}
SecureZeroMemory(pdg, dwDriverLayoutInfoExLen);
// set RewritePartition=true in every partition to force rewrite.
// for (int item = 0; item < 4; item++){
当PC中新增加硬盘或者添加了一个虚拟的磁盘设备时,通过windows的磁盘管理工具可以完成磁盘的初始化、磁盘分区格式化。现在希望在只知道磁盘设备(例如,\\\\.\\PhysicalDrive0)的前提下完成像windows磁盘管理工具一样的功能。现在通过DeviceIoControl API发送磁盘相关指令,但是代码存在以下问题:磁盘可以初始化,可以完成分区,但是磁盘初始化分区后的磁盘引导扇区数据不对。并且这部分代码没有格式化分区功能,代码如下:
int InitDisk(void)
{
BOOL bResult = FALSE; // generic results flag
CAutoFile hDevice; // handle to the drive to be examined
DWORD junk = 0; // discard results
hDevice = CreateFile(TEXT("\\\\.\\PhysicalDrive0"), //"\\\\.\\PhysicalDrive1", // drive to open
GENERIC_READ | GENERIC_WRITE, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return GetLastError();
}
CREATE_DISK dsk;
dsk.PartitionStyle = PARTITION_STYLE_MBR;
dsk.Mbr.Signature = 9999;
// 初始化磁盘
bResult = DeviceIoControl(hDevice, // device to be queried
IOCTL_DISK_CREATE_DISK, // operation to perform
&dsk, sizeof(dsk), //sizeof(pdg), // output buffer
NULL, 0, // no output buffer
&junk, // # bytes returned
NULL);
if (!bResult)
{
return GetLastError();
}
bResult = DeviceIoControl(hDevice,
IOCTL_DISK_UPDATE_PROPERTIES,
NULL, 0, NULL, 0, &junk, NULL);
if (! bResult)
{
return GetLastError();
}
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];
if (pdg == NULL)
{
return -1;
}
SecureZeroMemory(pdg, dwDriverLayoutInfoExLen);
// set RewritePartition=true in every partition to force rewrite.
// for (int item = 0; item < 4; item++){