windows的磁盘操作之一——基本概念

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-02 06:25 点击:

最近项目中需要在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

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • dos命令原来也能这样用?允许查找未知子目录指定文件
    •  windows的磁盘操作之八——格式化分区的思考
    • windows篇-第三章 安装和配置DNS服务器
    • windows篇-第二章 安装和配置Web服务器
    • windows篇-第一章 windows server 2008安装和简单配置
    •  windows server 2003断开远程之后自动注销用户
    • Windows服务器下用IIS Rewrite组件为IIS设置伪静态方法
    • Windows 2008 R2 SP1部署WSUS 3.0 SP2
    • windows 2008 rodc扩展

    快速直达

    操作系统导航

    LinuxWindows虚拟机
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1