Disk operation 3 in windows -- Obtain and delete disk partition information

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/624079

The previous section describes how to initialize a blank disk and create partitions. For a disk with an existing partition, how do we obtain its partition information and delete its partition information? This section discusses these two types of operations.

The code for obtaining disk partition information is as follows. /*************************************** **************************************** Function: get the disk's drive layout infomation * input: disk, disk name * output: Drive layout info * return: succeed, 0 * fail, -1 ************************************** **************************************** /DWORD getdiskdrivelayout (const char * disk, drive_layout_information_ex * drivelayout) {handle hdevice; // handle To the drive to be examined bool result; // results flag DWORD readed; // discard results hdevice = createfile (disk, // drive to open generic_read | generic_write, // access to the drive file_pai_read | file_pai_write, // share mode null, // default security attributes open_existing, // disposition 0, // file attributes null // do not copy file attribute); If (hdevice = invalid_handle_value) // Cannot open the drive {fprintf (stderr, "createfile () error: % LD \ n", getlasterror (); Return DWORD (-1 );} result = deviceiocontrol (hdevice, // handle to device handle, // dwiocontrolcode null, // lpinbuffer 0, // ninbuffersize drivelayout, // output buffer sizeof (* drivelayout ), // size of output buffer & readed, // number of bytes returned null // overlapped structure); If (! Result) {fprintf (stderr, "ioctl_disk_get_drive_layout_ex error: % LD \ n", getlasterror (); (void) closehandle (hdevice); Return DWORD (-1 );} (void) closehandle (hdevice); Return 0;} if you have a deep understanding of the Code http://cutebunny.blog.51cto.com/301216/624052 for creating partitions in the previous section, then this code is very simple. 1. Call createfile according to the disk name to open the device handle. 2. Call the deviceiocontrol function with the operation code ioctl_disk_get_drive_layout_ex to obtain the partition information. The returned information is stored in drive_layout_information_ex.
* Drivelayout. In this example, we only consider one partition. If there are multiple partitions, adjust the noutbuffersize parameter in the deviceiocontrol function. 3. parse * drivelayout to obtain the partition information. The code for deleting disk partition information is as follows, /*************************************** **************************************** function: delete the partition layout of the disk * input: disk, disk name * output: N/A * return: succeed, 0 * fail, -1 ************************************** **************************************** /DWORD destroydisk (DWORD disk) {handle hdevice; // handle to the drive to be examined bool result; // results f Lag DWORD readed; // discard results char diskpath [disk_path_len]; sprintf (diskpath ,"\\\\. \ physicaldrive % d ", disk); hdevice = createfile (diskpath, // drive to open generic_read | generic_write, // access to the drive file_0000_read | file_0000_write, // share mode null, // default security attributes open_existing, // disposition 0, // file attributes null // do not copy file attribute); If (Hdevice = invalid_handle_value) // cannot open the drive {fprintf (stderr, "createfile () error: % LD \ n", getlasterror ()); return DWORD (-1);} result = deviceiocontrol (hdevice, // handle to device ioctl_disk_delete_drive_layout, // dwiocontrolcode null, // lpinbuffer 0, // ninbuffersize null, // lpoutbuffer 0, // noutbuffersize & readed, // number of bytes returned null // overlapped structure); I F (! Result) {// fprintf (stderr, "ioctl_disk_delete_drive_layout 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 );} (void) closehandle (hdevice); Return 0;} the parameter DWORD disk is the physical drive letter. Function Execution Process: 1. Generate a device name based on the drive letter. 2. Call createfile to open the device and obtain the device handle. 3. Call the deviceiocontrol function of ioctl_disk_delete_drive_layout to delete the partition table. 4. Refresh the partition table. The status of the disk after destroydisk is called is

 

 

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

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.