用IO创建并格式化分区

时间:2023-03-08 22:58:34
用IO创建并格式化分区
  1. 转载:http://raylinn.iteye.com/blog/570274
  2. BOOL Result; // used to read bad DeviceIoControl calls
  3. DWORD szReturned;
  4. unsigned int SectorSize = 512;
  5. LARGE_INTEGER DiskSize.QuadPart = 40007761920i64;
  6. LARGE_INTEGER Part_1_size.QuadPart = 27406600704i64;
  7. LARGE_INTEGER Part_2_size.QuadPart =40007761920i64-27406600704i64;
  8. // Very important! Size correctly this structure. Even if there's only
  9. // one primary partition, you MUST size the buffer to contain
  10. // AT LEAST 4 PARTITION_INFORMATION_EX!
  11. DWORD szNewLayout = sizeof(DRIVE_LAYOUT_INFORMATION_EX)+4*sizeof(PARTITION_INFOR MATION_EX);
  12. DRIVE_LAYOUT_INFORMATION_EX *dl = (DRIVE_LAYOUT_INFORMATION_EX*) new BYTE[szNewLayout];
  13. // Open handle to physical device
  14. // NtCreateFile() function can be used too with "\\device\\harddisk1\\partiton0" path.
  15. hDrive=CreateFile("\\\\.\\PhysicalDrive1",GENERIC_READ|GEN ERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,
  16. NULL, //default security attributes
  17. OPEN_EXISTING, // disposition
  18. 0,// file attributes
  19. NULL);
  20. if(!hDrive){
  21. // handle the error
  22. }
  23. CREATE_DISK disk;
  24. ZeroMemory(&disk,sizeof(CREATE_DISK));
  25. disk.PartitionStyle = PARTITION_STYLE_MBR;
  26. disk.Mbr.Signature = 0xA4B57300;// the signature can be randomly generated
  27. // Create primary partition MBR
  28. Result = DeviceIoControl(hDrive,IOCTL_DISK_CREATE_DISK,&disk,size of(CREATE_DISK),NULL,0,&szReturned,NULL);
  29. if(!Result){
  30. // handle the error
  31. }
  32. DeviceIoControl(hDrive,IOCTL_DISK_UPDATE_PROPERTIES,
  33. NULL,0,NULL,0,&szReturned,NULL);
  34. //Setup drive layout
  35. ZeroMemory(dl,szNewLayout);
  36. dl->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;
  37. dl->PartitionEntry[0].StartingOffset.QuadPart = 32256;
  38. dl->PartitionEntry[0].PartitionLength = Part_1_Size;
  39. dl->PartitionEntry[0].PartitionNumber = 1;
  40. dl->PartitionEntry[0].RewritePartition = TRUE;
  41. dl->PartitionEntry[0].Mbr.PartitionType = 0x07;// PARTITION_IFS (NTFS partition or logical drive)
  42. dl->PartitionEntry[0].Mbr.BootIndicator = TRUE;
  43. dl->PartitionEntry[0].Mbr.RecognizedPartition = 1;
  44. dl->PartitionEntry[0].Mbr.HiddenSectors=32256/SectorSize;
  45. dl->PartitionEntry[1].PartitionStyle=PARTITION_STYLE_MBR;
  46. dl->PartitionEntry[1].StartingOffset.QuadPart= Part_1_Size.QuadPart + 32256i64;
  47. dl->PartitionEntry[1].PartitionLength = Part_2_Size;
  48. dl->PartitionEntry[1].PartitionNumber=2;
  49. dl->PartitionEntry[1].RewritePartition = TRUE;
  50. dl->PartitionEntry[1].Mbr.PartitionType = 0x07;
  51. dl->PartitionEntry[1].Mbr.RecognizedPartition = 1;
  52. dl->PartitionEntry[1].Mbr.HiddenSectors = (32256i64+Part_1_Size.QuadPart)/SectorSize;
  53. // set RewritePartition=true in every partition to force rewrite.
  54. for (int item=0;item<4;item++)
  55. dl->PartitionEntry[item].RewritePartition = 1;
  56. // setup drive layout
  57. dl->PartitionStyle = PARTITION_STYLE_MBR;
  58. dl->PartitionCount = 4;// specify AT LEAST 4 partitions!!!
  59. dl->Mbr.Signature = 0xA4B57300;
  60. // Set layout
  61. Result = DeviceIoControl(hDrive,IOCTL_DISK_SET_DRIVE_LAYOUT_EX,
  62. & ; ;nbs p;      dl,szNewLayout,NULL,0,& ; ; ;szReturned,NULL);
  63. if(!Result)
  64. throw Exception(WhatError());
  65. // update disk properties
  66. DeviceIoControl(hDrive,IOCTL_DISK_UPDATE_PROPERTIES,
  67. NULL,0,NULL,0,&szReturned,NULL);
  68. CloseHandle(hDrive);
  69. delete dl;