最近項(xiàng)目中需要在windows系統(tǒng)下與磁盤(pán)打交道,用了一個(gè)禮拜時(shí)間,弄懂了一些基本的概念,記錄于此,并以項(xiàng)目中的部分代碼作為范例。
首先說(shuō)明一點(diǎn),本文中使用的不是cmd命令行,基于以下幾點(diǎn)原因:
1.在C/C++中調(diào)用系統(tǒng)命令會(huì)存在處理的種種不方便,需要大量額外的代碼去分析命令執(zhí)行結(jié)果。
2.windows命令行遠(yuǎn)不如linux的shell來(lái)的強(qiáng)大。
3.效率。
當(dāng)然,如果不考慮編碼,僅作為系統(tǒng)下一種應(yīng)用工具的話(huà),DiskPart是既安全又便利的選擇。
我們先來(lái)看幾個(gè)主要的使用頻繁的函數(shù)。
在windows下與磁盤(pán)打交道最主要的API就是DeviceIoControl了,以下是從MSDN中直接拷貝出來(lái)的對(duì)該函數(shù)的說(shuō)明。此函數(shù)確實(shí)太重要也太強(qiáng)大了,建議大家耐著性子先將它的說(shuō)明看完,當(dāng)然,本文后續(xù)例子中會(huì)大量用到此函數(shù),可隨時(shí)返回此節(jié)參閱。
DeviceIoControl Function
Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.
BOOL WINAPI DeviceIoControl(
__in HANDLE hDevice,
__in DWORD dwIoControlCode,
__in LPVOID lpInBuffer,
__in DWORD nInBufferSize,
__out LPVOID lpOutBuffer,
__in DWORD nOutBufferSize,
__out LPDWORD lpBytesReturned,
__in LPOVERLAPPED lpOverlapped
。;
Parameters
hDevice
A handle to the device on which the operation is to be performed. The device is typically a volume, directory, file, or stream. To retrieve a device handle, use the CreateFile function. For more information, see Remarks.
dwIoControlCode
The control code for the operation. This value identifies the specific operation to be performed and the type of device on which to perform it.
For a list of the control codes, see Remarks. The documentation for each control code provides usage details for the lpInBuffer, nInBufferSize, lpOutBuffer, and nOutBufferSize parameters.
lpInBuffer
A pointer to the input buffer that contains the data required to perform the operation. The format of this data depends on the value of the dwIoControlCode parameter.
This parameter can be NULL if dwIoControlCode specifies an operation that does not require input data.
nInBufferSize
The size of the input buffer, in bytes.
lpOutBuffer
A pointer to the output buffer that is to receive the data returned by the operation. The format of this data depends on the value of the dwIoControlCode parameter.
This parameter can be NULL if dwIoControlCode specifies an operation that does not return data.
nOutBufferSize
The size of the output buffer, in bytes.
lpBytesReturned
A pointer to a variable that receives the size of the data stored in the output buffer, in bytes.
If the output buffer is too small to receive any data, the call fails, GetLastError returns ERROR_INSUFFICIENT_BUFFER, and lpBytesReturned is zero.
If the output buffer is too small to hold all of the data but can hold some entries, some drivers will return as much data as fits. In this case, the call fails, GetLastError returns ERROR_MORE_DATA, and lpBytesReturned indicates the amount of data received. Your application should call DeviceIoControl again with the same operation, specifying a new starting point.
If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an operation returns no output data and lpOutBuffer is NULL, DeviceIoControl makes use of lpBytesReturned. After such an operation, the value of lpBytesReturned is meaningless.
If lpOverlapped is not NULL, lpBytesReturned can be NULL. If this parameter is not NULL and the operation returns data, lpBytesReturned is meaningless until the overlapped operation has completed. To retrieve the number of bytes returned, call GetOverlappedResult. If hDevice is associated with an I/O completion port, you can retrieve the number of bytes returned by calling GetQueuedCompletionStatus.
lpOverlapped
A pointer to an OVERLAPPED structure.
If hDevice was opened without specifying FILE_FLAG_OVERLAPPED, lpOverlapped is ignored.
If hDevice was opened with the FILE_FLAG_OVERLAPPED flag, the operation is performed as an overlapped (asynchronous) operation. In this case, lpOverlapped must point to a valid OVERLAPPED structure that contains a handle to an event object. Otherwise, the function fails in unpredictable ways.
For overlapped operations, DeviceIoControl returns immediately, and the event object is signaled when the operation has been completed. Otherwise, the function does not return until the operation has been completed or an error occurs.
Return Value
If the operation completes successfully, the return value is nonzero.
If the operation fails or is pending, the return value is zero. To get extended error information, call GetLastError.
Remarks
To retrieve a handle to the device, you must call the CreateFile function with either the name of a device or the name of the driver associated with a device. To specify a device name, use the following format:
\\.\DeviceName
DeviceIoControl can accept a handle to a specific device. For example, to open a handle to the logical drive A: with CreateFile, specify \\.\a:. Alternatively, you can use the names \\.\PhysicalDrive0, \\.\PhysicalDrive1, and so on, to open handles to the physical drives on a system.
You should specify the FILE_SHARE_READ and FILE_SHARE_WRITE access flags when calling CreateFile to open a handle to a device driver. However, when you open a communications resource, such as a serial port, you must specify exclusive access. Use the other CreateFile parameters as follows when opening a device handle:
· The fdwCreate parameter must specify OPEN_EXISTING.
· The hTemplateFile parameter must be NULL.
· The fdwAttrsAndFlags parameter can specify FILE_FLAG_OVERLAPPED to indicate that the returned handle can be used in overlapped (asynchronous) I/O operations.
Requirements
Client
Requires Windows Vista, Windows XP, Windows 2000 Professional, or Windows NT Workstation.
Server
Requires Windows Server 2008, Windows Server 2003, Windows 2000 Server, or Windows NT Server.
Header
Declared in Winbase.h; include Windows.h.
Library
Use Kernel32.lib.
DLL
Requires Kernel32.dll.
該函數(shù)實(shí)現(xiàn)對(duì)設(shè)備的訪問(wèn),包括獲取信息,發(fā)送命令,交換數(shù)據(jù)等?梢岳迷摻涌诤瘮(shù)向指定的設(shè)備驅(qū)動(dòng)發(fā)送正確的控制碼及數(shù)據(jù),分析它的響應(yīng),執(zhí)行程序設(shè)計(jì)人員想要的功能。磁盤(pán)操作只是它強(qiáng)大功能中的一小部分。
該函數(shù)最重要的兩個(gè)參數(shù)是hDevice和dwIoControlCode.
控制碼dwIoControlCode決定了操作類(lèi)型,與磁盤(pán)相關(guān)的控制碼有
IOCTL_DISK_CREATE_DISK 利用CREATE_DISK結(jié)構(gòu)中的信息對(duì)指定磁盤(pán)和磁盤(pán)分區(qū)進(jìn)行初始化。
IOCTL_DISK_DELETE_DRIVE_LAYOUT 從主引導(dǎo)記錄中刪除引導(dǎo)信息,所以磁盤(pán)將會(huì)被從頭到尾的格式化。扇區(qū)0中的分區(qū)信息也就不復(fù)存在了。
IOCTL_DISK_FORMAT_TRACKS 格式化指定的、連續(xù)的軟盤(pán)磁道。如果需要更多的功能請(qǐng)使用IOCTL_DISK_FORMAT_TRACKS_EX。
IOCTL_DISK_FORMAT_TRACKS_EX 格式化指定的、連續(xù)的軟盤(pán)磁道。
IOCTL_DISK_GET_CACHE_INFORMATION 返回磁盤(pán)的高速緩存配置數(shù)據(jù)
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX 返回物理磁盤(pán)的擴(kuò)展信息。包括:類(lèi)型、柱面數(shù)量、每柱面磁道數(shù)、每磁道扇區(qū)數(shù)和每扇區(qū)字節(jié)數(shù)等。
IOCTL_DISK_GET_DRIVE_LAYOUT_EX 返回各分區(qū)的擴(kuò)展信息以及這些分區(qū)的特性。更多信息請(qǐng)參照DRIVE_LAYOUT_INFORMATION_EX結(jié)構(gòu)。
IOCTL_DISK_GET_LENGTH_INFO 返回指定磁盤(pán)/卷/分區(qū)的大小信息
IOCTL_DISK_GET_PARTITION_INFO_EX 返回指定分區(qū)的擴(kuò)展信息。包括:分區(qū)類(lèi)型、大小和種類(lèi)。更多信息請(qǐng)參照PARTITION_INFORMATION_EX結(jié)構(gòu)。
IOCTL_DISK_GROW_PARTITION 擴(kuò)大指定分區(qū)。
IOCTL_DISK_IS_WRITABLE 確定指定磁盤(pán)是否可寫(xiě)。
IOCTL_DISK_PERFORMANCE 啟用并獲取磁盤(pán)性能統(tǒng)計(jì)
IOCTL_DISK_PERFORMANCE_OFF 關(guān)閉磁盤(pán)性能統(tǒng)計(jì)
IOCTL_DISK_REASSIGN_BLOCKS 使磁盤(pán)設(shè)備影射一塊區(qū)域做為它的備用存儲(chǔ)塊公用池(spare block pool)。
IOCTL_DISK_SET_CACHE_INFORMATION 設(shè)置磁盤(pán)的配置信息
IOCTL_DISK_SET_DRIVE_LAYOUT_EX 根據(jù)給定的磁盤(pán)信息對(duì)磁盤(pán)進(jìn)行分區(qū)。
IOCTL_DISK_SET_PARTITION_INFO_EX 設(shè)置指定分區(qū)的分區(qū)信息。包括AT和EFI (Extensible Firmware Interface)分區(qū)的布局信息。
IOCTL_DISK_UPDATE_PROPERTIES 使緩沖的分區(qū)表無(wú)效并重新獲取一份。
IOCTL_DISK_VERIFY 對(duì)指定磁盤(pán)進(jìn)行邏輯格式化
另一個(gè)參數(shù)hDevice指向要操作的設(shè)備句柄,調(diào)用函數(shù)CreateFile獲得。CreateFile函數(shù)原型為
HANDLE WINAPI CreateFile(
__in LPCTSTR lpFileName,
__in DWORD dwDesiredAccess,
__in DWORD dwShareMode,
__in LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__in DWORD dwCreationDisposition,
__in
億恩科技地址(ADD):鄭州市黃河路129號(hào)天一大廈608室 郵編(ZIP):450008 傳真(FAX):0371-60123888
聯(lián)系:億恩小凡
QQ:89317007
電話(huà):0371-63322206 本文出自:億恩科技【mszdt.com】
服務(wù)器租用/服務(wù)器托管中國(guó)五強(qiáng)!虛擬主機(jī)域名注冊(cè)頂級(jí)提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|