上一節(jié)中介紹了如何初始化一塊空白的磁盤,并創(chuàng)建分區(qū)。那么對于一塊已存在分區(qū)的磁盤,我們?nèi)绾潍@得其分區(qū)信息,如何刪除其分區(qū)信息呢?本節(jié)對這兩類操作進行討論。
獲得磁盤分區(qū)信息的代碼如下。
/******************************************************************************
* 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_SHARE_READ | FILE_SHARE_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 ", GetLastError());
return DWORD(-1);
}
result = DeviceIoControl(
hDevice, // handle to device
IOCTL_DISK_GET_DRIVE_LAYOUT_EX, // 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 ", GetLastError());
。╲oid)CloseHandle(hDevice);
return DWORD(-1);
}
。╲oid)CloseHandle(hDevice);
return 0;
}
如果你已對上一節(jié)中創(chuàng)建分區(qū)的代碼有了比較深刻的了解,那么這段代碼就非常簡單了。程序執(zhí)行流程為:
1. 根據(jù)disk名稱調(diào)用CreateFile打開設(shè)備句柄。
2. 調(diào)用操作碼為IOCTL_DISK_GET_DRIVE_LAYOUT_EX的DeviceIoControl函數(shù)獲取分區(qū)信息。返回的信息存儲在DRIVE_LAYOUT_INFORMATION_EX *driveLayout中。本例中我們只考慮了一個分區(qū)的情況,如果有多個分區(qū),適當(dāng)調(diào)整DeviceIoControl函數(shù)中的nOutBufferSize參數(shù)即可。
3. 解析*driveLayout即可獲得分區(qū)信息。
刪除磁盤分區(qū)信息的代碼如下,
/******************************************************************************
* 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 flag
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_SHARE_READ | FILE_SHARE_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 ", 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
。;
if (!result)
{
//fprintf(stderr, "IOCTL_DISK_DELETE_DRIVE_LAYOUT Error: %ld ", GetLastError());
。╲oid)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 ", GetLastError());
。╲oid)CloseHandle(hDevice);
return DWORD(-1);
}
。╲oid)CloseHandle(hDevice);
return 0;
}
參數(shù)DWORD disk為物理驅(qū)動器號。函數(shù)執(zhí)行流程為:
1. 根據(jù)驅(qū)動器號生成設(shè)備名稱。
2. 調(diào)用CreateFile打開設(shè)備并獲得設(shè)備句柄。
3. 調(diào)用操作碼為IOCTL_DISK_DELETE_DRIVE_LAYOUT的DeviceIoControl函數(shù)刪除分區(qū)表。
4. 刷新分區(qū)表。
調(diào)用DestroyDisk后的磁盤在windows磁盤管理中的狀態(tài)為
億恩科技地址(ADD):鄭州市黃河路129號天一大廈608室 郵編(ZIP):450008 傳真(FAX):0371-60123888
聯(lián)系:億恩小凡
QQ:89317007
電話:0371-63322206 本文出自:億恩科技【mszdt.com】
服務(wù)器租用/服務(wù)器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|