| 关键词: nbsp disk INFORMATION PartitionEntry partition 分区表 DWORD LAYOUT drive hDevice |
上一节中我们介绍了一些基本概念和主要的API,本节开始我们将列举并分析一些实例。本文中的所有代码我都在vs2008下测试过,读者只需要替换少量的宏定义即可编译执行。 面对一块新的磁盘,我们首先要做的就是对其初始化。在系统中通过windows的磁盘管理完成这一点非常容易,但在程序中实现略微复杂。本节的示例代码对一块新硬盘初始化,并在上面创建分区。 代码如下: /****************************************************************************** * Function: initialize the disk and create partitions * input: disk, disk name * parNum, partition number * output: N/A * return: Succeed, 0 * Fail, -1 ******************************************************************************/ DWORD CreateDisk(DWORD disk, WORD partNum) { HANDLE hDevice; // handle to the drive to be examined BOOL result; // results flag DWORD readed; // discard results DWORD ret; WORD i; CHAR diskPath[DISK_PATH_LEN]; DISK_GEOMETRY pdg; DWORD sectorSize; DWORD signature; LARGE_INTEGER diskSize; LARGE_INTEGER partSize; BYTE actualPartNum; DWORD layoutStructSize; DRIVE_LAYOUT_INFORMATION_EX *dl; CREATE_DISK newDisk; sprintf(diskPath, "\\\\.\\PhysicalDrive%d", disk); actualPartNum = 4; if (partNum > actualPartNum) { return (WORD)-1; } hDevice = CreateFile( diskPath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, //default security attributes OPEN_EXISTING, // disposition 0, // file attributes NULL ); if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive { fprintf(stderr, "CreateFile() Error: %ld ", GetLastError()); return DWORD(-1); } // Create primary partition MBR newDisk.PartitionStyle = PARTITION_STYLE_MBR; signature = (DWORD)time(NULL); //get signature from current time newDisk.Mbr.Signature = signature; result = DeviceIoControl( hDevice, IOCTL_DISK_CREATE_DISK, &newDisk, sizeof(CREATE_DISK), NULL, 0, &readed, NULL ); if (!result) { fprintf(stderr, "IOCTL_DISK_CREATE_DISK Error: %ld ", GetLastError()); (void)CloseHandle(hDevice); return DWORD(-1); } //fresh the partition table result = DeviceIoControl( hDevice, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &readed, NULL ); if (!result) { fprintf(stderr, "IOCTL_DISK_UPDATE_PROPERTIES Error: %ld ", GetLastError()); (void)CloseHandle(hDevice); return DWORD(-1); } //Now create the partitions ret = GetDriveGeometry(diskPath, &pdg); if ((DWORD)-1 == ret) { return ret; } sectorSize = pdg.BytesPerSector; diskSize.QuadPart = pdg.Cylinders.QuadPart * pdg.TracksPerCylinder * pdg.SectorsPerTrack * pdg.BytesPerSector; //calculate the disk size; partSize.QuadPart = diskSize.QuadPart / partNum; layoutStructSize = sizeof(DRIVE_LAYOUT_INFORMATION_EX) + (actualPartNum - 1) * sizeof(PARTITION_INFORMATION_EX); dl = (DRIVE_LAYOUT_INFORMATION_EX*)malloc(layoutStructSize); if (NULL == dl) { (void)CloseHandle(hDevice); return (WORD)-1; } dl->PartitionStyle = (DWORD)PARTITION_STYLE_MBR; dl->PartitionCount = actualPartNum; dl->Mbr.Signature = signature; //clear the unused partitions for (i = 0; i < actualPartNum; i++){ dl->PartitionEntry[i].RewritePartition = 1; dl->PartitionEntry[i].Mbr.PartitionType = PARTITION_ENTRY_UNUSED; } //set the profile of the partitions for (i = 0; i < partNum; i++){ dl->PartitionEntry[i].PartitionStyle = PARTITION_STYLE_MBR; dl->PartitionEntry[i].StartingOffset.QuadPart = (partSize.QuadPart * i) + ((LONGLONG)(pdg.SectorsPerTrack) * (LONGLONG)(pdg.BytesPerSector)); //32256 dl->PartitionEntry[i].PartitionLength.QuadPart = partSize.QuadPart; dl->PartitionEntry[i].PartitionNumber = i + 1; dl->PartitionEntry[i].RewritePartition = TRUE; dl->PartitionEntry[i].Mbr.PartitionType = PARTITION_IFS; dl->PartitionEntry[i].Mbr.BootIndicator = FALSE; dl->PartitionEntry[i].Mbr.RecognizedPartition = TRUE; dl->PartitionEntry[i].Mbr.HiddenSectors = pdg.SectorsPerTrack + (DWORD)((partSize.QuadPart / sectorSize) * i); } //execute the layout result = DeviceIoControl( hDevice, IOCTL_DISK_SET_DRIVE_LAYOUT_EX, dl, layoutStructSize, NULL, 0, &readed, NULL ); if (!result) { fprintf(stderr, "IOCTL_DISK_SET_DRIVE_LAYOUT_EX Error: %ld ", GetLastError()); free(dl); (void)CloseHandle(hDevice); return DWORD(-1); } //fresh the partition table result = DeviceIoControl( hDevice, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &readed, NULL ); if (!result) { fprintf(stderr, "IOCTL_DISK_UPDATE_PROPERTIES Error: %ld ", GetLastError()); free(dl); (void)CloseHandle(hDevice); return DWORD(-1); } free(dl); (void)CloseHandle(hDevice); Sleep(3000); //wait the operations take effect return 0; } 函数CreateDisk包含两个参数, DWORD disk 填入物理驱动器号,参见第一节。 WORD partNum 表示需要创建的分区数,partNum <= 4。 函数的执行流程解释如下: /***************初始化磁盘*****************/ 1. 根据disk创建设备名称,\\\\.\\PhysicalDriveX,这里由于要转义,所以”\”都写为”\\”。 2. 调用CreateFile打开设备文件,并获得句柄。 3. 用操作码IOCTL_DISK_CREATE_DISK调用DeviceIoControl函数,初始化磁盘并创建分区表。 使用IOCTL_DISK_CREATE_DISK操作码时,lpInBuffer要填入一个CREATE_DISK结构参数,其中包括分区表类型和磁盘签名等参数,详见MSDN。本例中创建MBR分区表,签名由当前时间产生。 4. 刷新分区表。注意,程序中任何时候对磁盘的分区信息进行了修改都需要调用操作码为IOCTL_DISK_UPDATE_PROPERTIES的DeviceIoControl函数来刷新分区表,是操作切实生效。 /****************创建分区*******************/ 5. 调用GetDriveGeometry获取磁盘信息(GetDriveGeometry参见上一节)。由于创建分区时要填入分区大小信息,我们此处先计算磁盘总大小,然后除以partNum将字节数平均分配到各个分区。 6. 分配DRIVE_LAYOUT_INFORMATION_EX结构体空间。我们通过在这个结构体中填入数据来指定如何对硬盘进行分区。结构体定义如下 typedef struct _DRIVE_LAYOUT_INFORMATION_EX { DWORD PartitionStyle; DWORD PartitionCount; union { DRIVE_LAYOUT_INFORMATION_MBR Mbr; DRIVE_LAYOUT_INFORMATION_GPT Gpt; }; PARTITION_INFORMATION_EX PartitionEntry[1]; } DRIVE_LAYOUT_INFORMATION_EX, *PDRIVE_LAYOUT_INFORMATION_EX; 其中PartitionCount为4的倍数,为简化处理,我们这里定死为4。 另外还要注意PARTITION_INFORMATION_EX型的数组PartitionEntry[1]。虽然结构体中只定义了一个元素,但事实上必须在其后补足PartitionCount – 1个元素。所以代码中为DRIVE_LAYOUT_INFORMATION_EX *dl分配空间的时候加上了(actualPartNum - 1) * sizeof(PARTITION_INFORMATION_EX)。 7. 在DRIVE_LAYOUT_INFORMATION_EX结构体空间dl中填入数据。 先将所有分区都设为PARTITION_ENTRY_UNUSED,后面具体分配多少个分区再设置回来。 然后再循环体内对每个分区的PartitionEntry赋值,其中 StartingOffset除了跳过前面的分区已占据的空间外,还要加上63个扇区空间(32256字节)。 PartitionNumber从1开始。 Mbr.PartitionType = PARTITION_IFS表示NTFS格式。 Mbr.HiddenSectors MSDN上说The number of hidden sectors to be allocated when the partition table is created. 我理解得不是很深刻,欢迎补充。 8. 调用操作码为IOCTL_DISK_SET_DRIVE_LAYOUT_EX的DeviceIoControl函数执行分区,参数需要填入刚才准备好的DRIVE_LAYOUT_INFORMATION_EX结构体和大小。 9. 刷新分区表,原理同4。 另外,我在函数末尾加上了Sleep(3000)。这是因为我发现创建分区操作需要一定的执行时间,如果后续紧跟着其它相关操作(例如格式化该分区)可能会产生分区不存在的错误,所以此处等待3秒确保其执行完毕。 本节涉及的类型较多,但各类型具有很强的关联性,读者可随时查阅MSDN获得更详细的说明。 |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|