Disk operation 2 of windows -- initialize Disk

Source: Internet
Author: User
Original Works are allowed to be reprinted. During reprinting, please mark the article in hyperlink form
Original source, author information, and this statement. Otherwise, legal liability will be held. Http://cutebunny.blog.51cto.com/301216/624052

In the previous section, we introduced some basic concepts and main APIs. At the beginning of this section, we will list and analyze some instances. I have tested all the code in this article in vs2008. You only need to replace a few macro definitions to compile and execute the code.

In the face of a new disk, the first thing we need to do is to initialize it. It is easy to manage windows disks in the system, but it is slightly complicated to implement in the program. The sample code in this section initializes a new hard disk and creates a partition on it. The Code is as follows: /*************************************** **************************************** 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 [partition]; disk_geometry PDG; DWORD sectorsize; DWORD signature; large_integer disksize; large_integer partsize; byte partition; DWORD layoutstructsize; partition * 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_pai_read | file_pai_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 (bytes) + (actualpartnum-1) * sizeof (partition_information_ex); dl = (Bytes *) malloc (layoutstructstructsize); 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 ;} the createdisk function contains two parameters. DWORD disk is filled with the physical drive letter. See section 1. Word partnum indicates the number of partitions to be created, partnum <= 4. The function execution process is explained as follows: /*********************************/1. create a device name based on disk ,\\\\. \ physicaldrivex, which must be escaped. Therefore, "\" is written "\\". 2. Call createfile to open the device file and obtain the handle. 3. Call the deviceiocontrol function with the operation code ioctl_disk_create_disk to initialize the disk and create a partition table. When ioctl_disk_create_disk operation code is used, lpinbuffer must fill in a create_disk structure parameter, including the Partition Table type and disk signature parameters. For details, see msdn. In this example, create an MBR partition table. The signature is generated at the current time. 4. Refresh the partition table. Note: Any modification to the disk partition information in the program requires the deviceiocontrol function with the operation code ioctl_disk_update_properties to be called to refresh the partition table, which is effective. /************************************/5. call getdrivegeometry To Get disk information (getdrivegeometry see the previous http://cutebunny.blog.51cto.com/301216/624027 ). Because the partition size information is required during partition creation, we calculate the total disk size and divide it by partnum to evenly distribute the number of bytes to each partition. 6. Allocate the drive_layout_information_ex struct space. We specify how to partition the hard disk by entering data in this struct. The struct is defined as follows: typedef struct _ drive_layout_information_ex {DWORD partitionstyle; DWORD partitioncount; Union {drive_layout_information_mbr; drive_layout_information_gpt GPT ;}; partition_information_ex
Partitionentry [1];} drive_layout_information_ex, * pdrive_layout_information_ex; where partitioncount is a multiple of 4. to simplify the processing, we set it to 4 here. In addition, pay attention to the partition_information_ex array partitionentry [1]. Although only one element is defined in the struct, it must be supplemented with partitioncount.
-1 element. Therefore, the Code adds (actualpartnum-1) * sizeof (partition_information_ex) to the space allocated by drive_layout_information_ex * dl ). 7. Fill in data in the DL Of The drive_layout_information_ex struct space. Set all partitions to partition_entry_unused, and then specify the number of partitions allocated. The recycle body then assigns a value to the partitionentry of each partition. In addition to skipping the occupied space of the preceding partition, startingoffset also adds 63 slice spaces (32256 bytes ). Partitionnumber starts from 1. MBR. partitiontype = partition_ifs indicates the NTFS format. MBR. hiddensectors the number of hidden sectors to be allocated when the partition table is created on msdn.
I don't understand it very well. Please add. 8. Call the deviceiocontrol function of the operation code ioctl_disk_set_drive_layout_ex to execute the partition. The parameter must be filled with the ready drive_layout_information_ex struct and size. 9. Refresh the partition table in the same principle as 4. In addition, I added sleep (3000) to the end of the function ). This is because I found that the partition creation operation takes some time to execute. If other related operations (such as formatting the partition) are followed in the future, the error that the partition does not exist may occur, so wait 3 seconds to make sure the execution is complete. This section involves many types, but each type has a strong relevance. You can refer to msdn For more details at any time.

 

This article is from the "bunny Technology Workshop" blog, please be sure to keep this source http://cutebunny.blog.51cto.com/301216/624052

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.