始创于2000年 股票代码:831685
咨询热线:0371-60135900 注册有礼 登录
  • 挂牌上市企业
  • 60秒人工响应
  • 99.99%连通率
  • 7*24h人工
  • 故障100倍补偿
您的位置: 网站首页 > 帮助中心>文章内容

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

发布时间:  2012/9/11 19:38:46

  最近项目中需要在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, 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.
  该函数实现对设备的访问,包括获取信息,发送命令,交换数据等。可以利用该接口函数向指定的设备驱动发送正确的控制码及数据,分析它的响应,执行程序设计人员想要的功能。磁盘操作只是它强大功能中的一小部分。
  该函数最重要的两个参数是hDevice和dwIoControlCode.
  控制码dwIoControlCode决定了操作类型,与磁盘相关的控制码有
  IOCTL_DISK_CREATE_DISK    利用CREATE_DISK结构中的信息对指定磁盘和磁盘分区进行初始化。
  IOCTL_DISK_DELETE_DRIVE_LAYOUT    从主引导记录中删除引导信息,所以磁盘将会被从头到尾的格式化。扇区0中的分区信息也就不复存在了。
  IOCTL_DISK_FORMAT_TRACKS    格式化指定的、连续的软盘磁道。如果需要更多的功能请使用IOCTL_DISK_FORMAT_TRACKS_EX。
  IOCTL_DISK_FORMAT_TRACKS_EX    格式化指定的、连续的软盘磁道。
  IOCTL_DISK_GET_CACHE_INFORMATION    返回磁盘的高速缓存配置数据
  IOCTL_DISK_GET_DRIVE_GEOMETRY_EX    返回物理磁盘的扩展信息。包括:类型、柱面数量、每柱面磁道数、每磁道扇区数和每扇区字节数等。
  IOCTL_DISK_GET_DRIVE_LAYOUT_EX    返回各分区的扩展信息以及这些分区的特性。更多信息请参照DRIVE_LAYOUT_INFORMATION_EX结构。
  IOCTL_DISK_GET_LENGTH_INFO    返回指定磁盘/卷/分区的大小信息
  IOCTL_DISK_GET_PARTITION_INFO_EX    返回指定分区的扩展信息。包括:分区类型、大小和种类。更多信息请参照PARTITION_INFORMATION_EX结构。
  IOCTL_DISK_GROW_PARTITION    扩大指定分区。
  IOCTL_DISK_IS_WRITABLE    确定指定磁盘是否可写。
  IOCTL_DISK_PERFORMANCE    启用并获取磁盘性能统计
  IOCTL_DISK_PERFORMANCE_OFF    关闭磁盘性能统计
  IOCTL_DISK_REASSIGN_BLOCKS    使磁盘设备影射一块区域做为它的备用存储块公用池(spare block pool)。
  IOCTL_DISK_SET_CACHE_INFORMATION    设置磁盘的配置信息
  IOCTL_DISK_SET_DRIVE_LAYOUT_EX    根据给定的磁盘信息对磁盘进行分区。
  IOCTL_DISK_SET_PARTITION_INFO_EX    设置指定分区的分区信息。包括AT和EFI (Extensible Firmware Interface)分区的布局信息。
  IOCTL_DISK_UPDATE_PROPERTIES    使缓冲的分区表无效并重新获取一份。
  IOCTL_DISK_VERIFY    对指定磁盘进行逻辑格式化
  另一个参数hDevice指向要操作的设备句柄,调用函数CreateFile获得。CreateFile函数原型为
  HANDLE WINAPI CreateFile(
  __in          LPCTSTR lpFileName,
  __in          DWORD dwDesiredAccess,
  __in          DWORD dwShareMode,
  __in          LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  __in          DWORD dwCreationDisposition,
  __in      

亿恩科技地址(ADD):郑州市黄河路129号天一大厦608室 邮编(ZIP):450008 传真(FAX):0371-60123888
   联系:亿恩小凡
   QQ:89317007
   电话:0371-63322206


本文出自:亿恩科技【www.enkj.com】

服务器租用/服务器托管中国五强!虚拟主机域名注册顶级提供商!15年品质保障!--亿恩科技[ENKJ.COM]

  • 您可能在找
  • 亿恩北京公司:
  • 经营性ICP/ISP证:京B2-20150015
  • 亿恩郑州公司:
  • 经营性ICP/ISP/IDC证:豫B1.B2-20060070
  • 亿恩南昌公司:
  • 经营性ICP/ISP证:赣B2-20080012
  • 服务器/云主机 24小时售后服务电话:0371-60135900
  • 虚拟主机/智能建站 24小时售后服务电话:0371-60135900
  • 专注服务器托管17年
    扫扫关注-微信公众号
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 亿恩科技 版权所有  地址:郑州市高新区翠竹街1号总部企业基地亿恩大厦  法律顾问:河南亚太人律师事务所郝建锋、杜慧月律师   京公网安备41019702002023号
      0
     
     
     
     

    0371-60135900
    7*24小时客服服务热线