|
如题,在MFC中对多个磁盘进行初始化,代码如下:
- 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\n", 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\n", 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\n", 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\n", 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\n", GetLastError());
- free(dl);
- (void)CloseHandle(hDevice);
- return DWORD(-1);
- }
-
- free(dl);
- (void)CloseHandle(hDevice);
- Sleep(3000); //wait the operations take effect
- return 0;
- }
复制代码
以上代码是放进多线程里,然后,在多线程里运行时,对多个磁盘进行初始化,会出现某个磁盘脱机的现象。请问各位大佬,有没有办法解决这个问题?或者以上代码,怎么修改,才能满足初始化多个磁盘并且不会出现脱机的现象呢?劳烦各位大佬们看看,给点意见,小弟在此多谢了!
|
上一篇: 关于C++ Primer第五版P289练习8.10下一篇: 用MFC如何将硬盘从脱机状态变成联机状态
|