应用程序和驱动程序的通信过程是:应用程序使用CreateFile函数打开设备,然后用DeviceIoControl与驱动程序进行通信,包括读和写两种操作。还可以用ReadFile读数据用WriteFile写数据。操作完毕时用CloseHandle关闭设备。我们比较常用的就是用DeviceIoControl对设备进行读写操作。先看看DeviceIoControl是怎么定义的:

BOOL DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize, LPDWORD lpBytesReturned, LPOVERLAPPED lpOverlapped
);

Parameters(参数)

hDevice (CreateFile返回的设备句柄)

[in] Handle to the device that is to perform the operation. To obtain a device handle, call the CreateFile function.

dwIoControlCode (应用程序调用驱动程序的控制命令,就是IOCTL_XXX IOCTLs )

[in] IOCTL for the operation. This value identifies the specific operation to perform and the type of device on which to perform the operation. There are no specific values defined for the dwIoControlCode parameter. However, you can define custom IOCTL_XXX IOCTLs with the CTL_CODE macro. You can then advertise these IOCTLs and an application can use these IOCTLs with DeviceIoControl to perform the driver-specific functions.

lpInBuffer (应用程序传递给驱动程序的数据缓冲区地址)

[in] Long pointer to a buffer that contains the data required to perform the operation. Set to NULL if the dwIoControlCode parameter specifies an operation that does not require input data.

nInBufferSize (应用程序传递给驱动程序的数据缓冲区大小,字节数)

[in] Size, in bytes, of the buffer pointed to by lpInBuffer.

lpOutBuffer (驱动程序返回给应用程序的数据缓冲区地址)

[out] Long pointer to a buffer that receives the output data for the operation. Set to NULL if the dwIoControlCodeparameter specifies an operation that does not produce output data.

nOutBufferSize (驱动程序返回给应用程序的数据缓冲区大小,字节数)

[out] Size, in bytes, of the buffer pointed to by lpOutBuffer.

lpBytesReturned (驱动程序实际返回给应用程序的数据字节数地址)

[out] Long pointer to a variable that receives the size, in bytes, of the data stored in lpOutBuffer. The DeviceIoControl function may unnecessarily use this parameter. For example, if an operation does not produce data for lpOutBuffer and lpOutBuffer is NULL, the value of lpBytesReturned is meaningless.

lpOverlapped (重叠操作结构)

[in] Ignored; set to NULL.

Return Values(返回值)

Nonzero indicates success. Zero indicates failure. To obtain extended error information, call the GetLastErrorfunction. (非0成功,0失败)

具体使用我们看看列子:

1,向设备传递数据,我们定义一个函数来实现

bool CDeviceOperDlg::SendKeyData(HANDLE handle, BYTE *bData, int iSize)
{
 ULONG nOutput;
 BYTE bTemp[512];

//将数据放置到发送数组
 memset(bTemp,0,sizeof(bTemp));
 memcpy(bTemp,&bData[0],iSize);
 //向设备发送
 if (!DeviceIoControl(handle,         
       ATST2004_IOCTL_WRITE,     //根据具体的设备有相关的定义
       bTemp,                                        //向设备传递的数据地址
       iSize,                                            //数据大小,字节数
       NULL,                                          //没有返回的数据,置为NULL
       0,                                                  //没有返回的数据,置为0

&nOutput,
       NULL)
    )
 {
  return false;
 }

return true;
}

2,从设备读取数据

bool CDeviceOperDlg::ReviceKeyData(HANDLE handle, BYTE *bData, int iSize)
{

ULONG nOutput;
 BYTE bTemp[512];
 //数组清零
 memset(bTemp,0,sizeof(bTemp));
 //向设备发送
 if (!DeviceIoControl(handle,
       ATST2004_IOCTL_READ,           //根据具体的设备有相关的定义
       NULL,                                              //没有向设备传递的数据,置为NULL
       0,                                                      //没有向设备传递的数据,置为NULL
       bTemp,                                           //读取设备的数据返回地址
       iSize,                                               //读取数据的字节数
       &nOutput,
       NULL)
    )
 {
  return false;
 }
 //放置到公用数组
 memcpy(&bData[0],&bTemp[0],iSize);
 return true;
}


MSDN原文地址:DeviceIoControl function

DeviceIoControl 将控制代码直接发送到指定的设备驱动程序,使相应的设备执行相应的操作。

句法:

BOOL WINAPI DeviceIoControl(_In_ HANDLE hDevice,_In_ DWORD dwIoControlCode,_In_opt_ LPVOID lpInBuffer,_In_ DWORD nInBufferSize,_Out_opt_ LPVOID lpOutBuffer,_In_ DWORD nOutBufferSize,_Out_opt_ LPDWORD lpBytesReturned,_Inout_opt_ LPOVERLAPPED lpOverlapped);

参数:

hDevice [in]
    需要执行操作的设备句柄。该设备通常是卷,目录,文件或流,使用 CreateFile 函数打开获取设备句柄。具体的见备注
dwIoControlCode [in]
    操作的控制代码,该值标识要执行的特定操作以及执行该操作的设备的类型,有关控制代码的列表,请参考备注。每个控制代码的文档都提供了lpInBuffernInBufferSizelpOutBuffernOutBufferSize参数的使用细节。
lpInBuffer [in, optional]
    (可选)指向输入缓冲区的指针。这些数据的格式取决于dwIoControlCode参数的值。如果dwIoControlCode指定不需要输入数据的操作,则此参数可以为NULL。
nInBufferSize [in]
    输入缓冲区以字节为单位的大小。单位为字节。
lpOutBuffer [out, optional]
    (可选)指向输出缓冲区的指针。这些数据的格式取决于dwIoControlCode参数的值。如果dwIoControlCode指定不返回数据的操作,则此参数可以为NULL。
nOutBufferSize [in]
    输出缓冲区以字节为单位的大小。单位为字节。
lpBytesReturned [out, optional]
    (可选)指向一个变量的指针,该变量接收存储在输出缓冲区中的数据的大小。如果输出缓冲区太小,无法接收任何数据,则GetLastError返回ERROR_INSUFFICIENT_BUFFER,错误代码122(0x7a),此时lpBytesReturned是零。
    如果输出缓冲区太小而无法保存所有数据,但可以保存一些条目,某些驱动程序将返回尽可能多的数据,在这种情况下,调用失败,GetLastError返回ERROR_MORE_DATA,错误代码234lpBytesReturned指示接收到的数据量。您的应用程序应该再次使用相同的操作调用DeviceIoControl,指定一个新的起点。
    如果lpOverlapped为NULL,则lpBytesReturned不能为NULL。 即使操作没有返回输出数据并且lpOutBuffer为NULL,DeviceIoControl也会使用lpBytesReturned。在这样的操作之后,lpBytesReturned的值是没有意义的。
    如果lpOverlapped不为NULL,则lpBytesReturned可以为NULL。 如果此参数不为NULL并且操作返回数据,则在重叠操作完成之前,lpBytesReturned是无意义的。要检索返回的字节数,请调用GetOverlappedResult,如果hDevice与I / O完成端口相关联,则可以检索通过调用GetQueuedCompletionStatus返回的字节数。
lpOverlapped [in, out, optional]
    (可选)指向OVERLAPPED结构的指针,
  如果在未指定FILE_FLAG_OVERLAPPED的情况下打开hDevice,则忽略lpOverlapped
  如果使用FILE_FLAG_OVERLAPPED标志打开hDevice,则该操作将作为重叠(异步)操作执行。在这种情况下,lpOverlapped必须指向包含事件对象句柄的有效OVERLAPPED结构。 否则,该功能将以不可预知的方式失败。
    对于重叠操作,DeviceIoControl会立即返回,并在操作完成时通知事件对象。 否则,该功能在操作完成或发生错误之前不会返回。

返回值:
    如果操作成功完成,DeviceIoControl将返回一个非零值。

如果操作失败或正在等待,则DeviceIoControl返回零。 要获得扩展的错误信息,请调用GetLastError。

备注:
    要检索设备句柄,必须使用设备名称或与设备关联的驱动程序名称来调用CreateFile函数。 要指定设备名称,请使用以下格式:
  \\.\DeviceName

DeviceIoControl可以接受特定设备的句柄。 对于硬盘来说,可能为以下几种形式:

对于物理驱动器x,形式为 \\.\PhysicalDriveX ,编号从0开始,例如:

名称 含义
\\.\PhysicalDrive0   打开第一个物理驱动器
\\.\PhysicalDrive1 打开第二个物理驱动器

对于逻辑分区(卷),形式为 \\.\X: ,例如:

名称                          含义
\\.\A:  打开A盘(软驱)
\\.\C: 打开C盘(磁盘逻辑分区)

调用CreateFile打开设备驱动程序的句柄时,应指定FILE_SHARE_READFILE_SHARE_WRITE访问标志。但是,当您打开通信资源(如串行端口)时,必须指定独占访问权限。 打开设备句柄时使用其他CreateFile参数,如下所示:
     (a)fdwCreate参数必须指定OPEN_EXISTING
     (b)hTemplateFile参数必须为NULL。

(c)fdwAttrsAndFlags参数可指定FILE_FLAG_OVERLAPPED,以指示返回的句柄可用于重叠(异步)I / O操作。

有关支持的控制代码列表,请参阅以下主题:
    Communications Control Codes
    Device Management Control Codes
    Directory Management Control Codes
    Disk Management Control Codes
    File Management Control Codes
    Power Management Control Codes
    Volume Management Control Codes

实例:
    有关使用DeviceIoControl的示例,请参阅调用DeviceIoControl。

其他链接:

CreateEvent
    CreateFile
    Device Input and Output Control (IOCTL)
    GetOverlappedResult
    GetQueuedCompletionStatus

OVERLAPPED


Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.

Syntax

C++复制

BOOL DeviceIoControl(HANDLE       hDevice,DWORD        dwIoControlCode,LPVOID       lpInBuffer,DWORD        nInBufferSize,LPVOID       lpOutBuffer,DWORD        nOutBufferSize,LPDWORD      lpBytesReturned,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 lpInBuffernInBufferSizelpOutBuffer, 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 returnsERROR_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 DeviceIoControlagain with the same operation, specifying a new starting point.

If lpOverlapped is NULLlpBytesReturned cannot be NULL. Even when an operation returns no output data and lpOutBuffer is NULLDeviceIoControl makes use of lpBytesReturned. After such an operation, the value of lpBytesReturned is meaningless.

If lpOverlapped is not NULLlpBytesReturned 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_OVERLAPPEDlpOverlapped 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 OVERLAPPEDstructure 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, callGetLastError.

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:

\.<i>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 CreateFileto 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.

For lists of supported control codes, see the following topics:

  • Communications Control Codes
  • Device Management Control Codes
  • Directory Management Control Codes
  • Disk Management Control Codes
  • File Management Control Codes
  • Power Management Control Codes
  • Volume Management Control Codes

Examples

For an example that uses DeviceIoControl, see Calling DeviceIoControl.

Requirements

   
Minimum supported client Windows XP
Minimum supported server Windows Server 2003
Target Platform Windows
Header ioapiset.h (include Windows.h)
Library Kernel32.lib
DLL Kernel32.dll

See Also

CreateEvent

CreateFile

Device Input and Output Control (IOCTL)

GetOverlappedResult

GetQueuedCompletionStatus

OVERLAPPED

DeviceIoControl相关推荐

  1. 实战DeviceIoControl 之中的一个:通过API訪问设备驱动程序

    Q 在NT/2000/XP中,我想用VC编写应用程序訪问硬件设备,如获取磁盘參数.读写绝对扇区数据.測试光驱实际速度等,该从哪里入手呢? A 在NT/2000/XP中,应用程序能够通过API函数Dev ...

  2. windows 驱动和应用层的三种通信方式 r3到r0 DeviceIoControl 4种ioctl请求

    驱动程序和客户应用程序经常需要进行数据交换,但我们知道驱动程序和客户应用程序可能不在同一个地址空间,因此操作系统必须解决两者之间的数据交换. 驱动层和应用层通信,主要是靠DeviceIoControl ...

  3. DeviceIoControl的使用说明

    应用程序和驱动程序的通信过程是:应用程序使用CreateFile函数打开设备,然后用DeviceIoControl与驱动程序进行通信,包括读和写两种操作.还可以用ReadFile读数据用WriteFi ...

  4. DeviceIOControl详解-各个击破

    DeviceIoControl这个api我们用的不多,但是很重要,有时会帮助我们实现一些特别的需求, 如获取硬件设备信息.与硬件设备通信(读写数据)等,对照msdn,下面我们详细解释一下这个api的用 ...

  5. DeviceIOControl具体解释-各个击破

    DeviceIoControl这个api我们用的不多,可是非常重要,有时会帮助我们实现一些特别的需求, 如获取硬件设备信息.与硬件设备通信(读写数据)等,对比msdn,以下我们详解一下这个api的使用 ...

  6. DeviceIoControl 错误码:error code 87 问题解决

    DeviceIoControl 用于和应用层和驱动层之间的数据传送.是应用层调用驱动文件(SYS)中的控制请求的重要方法. 今天在使用DeviceIoControl 调用一个自行编写的驱动程序的使用返 ...

  7. MFC: DeviceIoControl 通过API访问设备驱动程序

    转载:http://m.blog.csdn.net/article/details?id=21602051 DeviceIoControl的其实和ReadFile和WriteFile是一样的, 不过这 ...

  8. DeviceIoControl接口

    DeviceIoControl接口 在这一节中我们将要关于学习动态VXD,特别是如何创建,加载和使用. 点击这里下载例子 VxD接口 VxD总共提供了4种接口. l         VxD servi ...

  9. DeviceIoControl获取文件LCN

    在灾备项目组做CDP(Continual Data Protection),刚刚入门,最近看了下关于磁盘的操作,记录一下DeviceIoControl(). NTFS中的文件结构 NTFS文件系统中, ...

  10. DeviceIoControl使用说明

    DeviceIoControl的使用说明 16:29 15972人阅读 评论(0) 收藏 举报 应用程序和驱动程序的通信过程是:应用程序使用CreateFile函数打开设备,然后用DeviceIoCo ...

最新文章

  1. Django 图片上传upload_to路径指定失效的问题记录
  2. java中 fff_Java:要实现记录当前时间为yyyymmddhhmissfff的时间戳该怎么简单实现呢=。=...
  3. 状态同步与帧同步的简介
  4. 算法题目中常见的几种输入小点-gets,cin,scanf,getline,sstream
  5. 祝我们的所有女孩子,女生节快乐~
  6. PS教程第二十一课:有了选区就有了界限
  7. 父亲节重读朱自清先生的《背影》
  8. linux 测试网络_Linux后台开发:Linux命令系列18-ping测试网络
  9. 《R语言实战》读书笔记--学习张丹日志
  10. 用以太坊区块链保证Asp.Net Core的API安全(上)
  11. gFTP不能显示中文文件(夹)名的解决方法
  12. C语身教程第十章:文件(8)
  13. MFC中获取各种类指针的方法 (转)
  14. Java课设——文本编辑器
  15. android开发中常用的Eclipse快捷键详细整理
  16. FBEC2020大会圆满闭幕!区块链分会场人气爆棚!
  17. 电脑读取不U盘,在磁盘管理器中显示“无媒体”解决方法
  18. Go、Rust、C++和Zig语言的生产力对比 | Gopher Daily (2021.03.28) ʕ◔ϖ◔ʔ
  19. 创业者该怎么快准狠抓住知识付费项目这个机会变现?
  20. 淘宝客导购产品设计(二)

热门文章

  1. 数据结构--时间复杂度的算法
  2. msn无法登陆解决一则
  3. SQL与T-SQL的区别
  4. 入门学习爬取贴吧图片(附完整代码),2021/1/27有效
  5. 斯蒂夫乔布斯传札记:第三波
  6. 程序员如何挑选自己的专业方向
  7. 复杂网络研究常用载体集
  8. c程序设计语言实现itoa,C语言中实现itoa函数的实例
  9. FileSystemObject模型-如何列出目录中的文件(VB6)
  10. Hibernate 中Criteria Query查询详解