用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.   
  9. // Very important! Size correctly this structure. Even if there's only   
  10. // one primary partition, you MUST size the buffer to contain   
  11. // AT LEAST 4 PARTITION_INFORMATION_EX!  
  12. DWORD szNewLayout = sizeof(DRIVE_LAYOUT_INFORMATION_EX)+4*sizeof(PARTITION_INFOR MATION_EX);  
  13.   
  14. DRIVE_LAYOUT_INFORMATION_EX *dl = (DRIVE_LAYOUT_INFORMATION_EX*) new BYTE[szNewLayout];  
  15.   
  16. // Open handle to physical device  
  17. // NtCreateFile() function can be used too with "\\device\\harddisk1\\partiton0" path.  
  18. hDrive=CreateFile("\\\\.\\PhysicalDrive1",GENERIC_READ|GEN ERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,  
  19.          NULL, //default security attributes  
  20.          OPEN_EXISTING, // disposition  
  21.          0,// file attributes  
  22.          NULL);   
  23.    
  24. if(!hDrive){  
  25.         // handle the error  
  26.     }  
  27.   
  28.   
  29. CREATE_DISK disk;  
  30. ZeroMemory(&disk,sizeof(CREATE_DISK));  
  31. disk.PartitionStyle = PARTITION_STYLE_MBR;  
  32. disk.Mbr.Signature = 0xA4B57300;// the signature can be randomly generated  
  33.   
  34. // Create primary partition MBR  
  35.  Result = DeviceIoControl(hDrive,IOCTL_DISK_CREATE_DISK,&disk,size of(CREATE_DISK),NULL,0,&szReturned,NULL);   
  36.  if(!Result){  
  37.         // handle the error  
  38.     }  
  39. DeviceIoControl(hDrive,IOCTL_DISK_UPDATE_PROPERTIES,  
  40.         NULL,0,NULL,0,&szReturned,NULL);  
  41.   
  42.  //Setup drive layout  
  43.  ZeroMemory(dl,szNewLayout);  
  44.  dl->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;  
  45.  dl->PartitionEntry[0].StartingOffset.QuadPart = 32256;  
  46.  dl->PartitionEntry[0].PartitionLength = Part_1_Size;  
  47.  dl->PartitionEntry[0].PartitionNumber = 1;  
  48.  dl->PartitionEntry[0].RewritePartition = TRUE;  
  49.  dl->PartitionEntry[0].Mbr.PartitionType = 0x07;// PARTITION_IFS (NTFS partition or logical drive)  
  50.  dl->PartitionEntry[0].Mbr.BootIndicator = TRUE;  
  51.  dl->PartitionEntry[0].Mbr.RecognizedPartition = 1;  
  52.  dl->PartitionEntry[0].Mbr.HiddenSectors=32256/SectorSize;  
  53.   
  54.  dl->PartitionEntry[1].PartitionStyle=PARTITION_STYLE_MBR;  
  55.  dl->PartitionEntry[1].StartingOffset.QuadPart= Part_1_Size.QuadPart + 32256i64;  
  56.  dl->PartitionEntry[1].PartitionLength = Part_2_Size;  
  57.  dl->PartitionEntry[1].PartitionNumber=2;  
  58.  dl->PartitionEntry[1].RewritePartition = TRUE;  
  59.  dl->PartitionEntry[1].Mbr.PartitionType = 0x07;  
  60.  dl->PartitionEntry[1].Mbr.RecognizedPartition = 1;  
  61.  dl->PartitionEntry[1].Mbr.HiddenSectors = (32256i64+Part_1_Size.QuadPart)/SectorSize;  
  62.   
  63.  // set RewritePartition=true in every partition to force rewrite.  
  64.  for (int item=0;item<4;item++)  
  65.      dl->PartitionEntry[item].RewritePartition = 1;  
  66.   
  67.  // setup drive layout  
  68.  dl->PartitionStyle = PARTITION_STYLE_MBR;  
  69.  dl->PartitionCount = 4;// specify AT LEAST 4 partitions!!!  
  70.  dl->Mbr.Signature = 0xA4B57300;  
  71.   
  72.  // Set layout  
  73.  Result = DeviceIoControl(hDrive,IOCTL_DISK_SET_DRIVE_LAYOUT_EX,  
  74.          & ; ;nbs p;      dl,szNewLayout,NULL,0,& ; ; ;szReturned,NULL);  
  75.  if(!Result)  
  76.         throw Exception(WhatError());  
  77.   
  78.  // update disk properties  
  79.  DeviceIoControl(hDrive,IOCTL_DISK_UPDATE_PROPERTIES,  
  80.         NULL,0,NULL,0,&szReturned,NULL);  
  81.   
  82. CloseHandle(hDrive);  
  83. delete dl;  
posted @ 2018-04-17 14:35  飘雪歪歪  阅读(304)  评论(0编辑  收藏  举报