windows的磁盘操作之一——基本概念
最近项目中需要在windows系统下与磁盘打交道,用了一个礼拜时间,弄懂了一些基本的概念,记录于此,并以项目中的部分代码作为范例。
首先说明一点,本文中使用的不是cmd命令行,基于以下几点原因:
1.在C/C++中调用系统命令会存在处理的种种不方便,需要大量额外的代码去分析命令执行结果。
2.windows命令行远不如linux的shell来的强大。
3.效率。
当然,如果不考虑编码,仅作为系统下一种应用工具的话,DiskPart是既安全又便利的选择。
我们先来看几个主要的使用频繁的函数。
在windows下与磁盘打交道最主要的API就是DeviceIoControl了,以下是从MSDN中直接拷贝出来的对该函数的说明。此函数确实太重要也太强大了,建议大家耐着性子先将它的说明看完,当然,本文后续例子中会大量用到此函数,可随时返回此节参阅。
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, lpBy
- 发表评论
-
- 最新评论 更多>>