如何用DeviceIOControl读取并口信息

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

BOOL DeviceIoControl(

HANDLE hDevice,// (CreateFile返回的设备句柄)

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

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 dwIoControlCode parameter 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 GetLastError function. (非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;

}可以私聊我~

USB之WDM架构驱动中DeviceIoControl读取细节

deviceiocontrol(deviceiocontrol函数)相关推荐

  1. DeviceIOControl函数

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

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

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

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

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

  4. DeviceIOControl实战

    实战DeviceIoControl 之一:通过API访问设备驱动程序 Q 在NT/2000/XP中,我想用VC编写应用程序访问硬件设备,如获取磁盘参数.读写绝对扇区数据.测试光驱实际速度等,该从哪里入 ...

  5. DeviceIoControl 函数详细解析

    前言: 最近需要对Windows中的设备进行编程操作,其中涉及到非常重要的函数DeviceIoControl,在使用的时候也比较的复杂,国内这一块中文资料比较少,在学习之余顺便将其翻译出来,以供参考, ...

  6. 应用程序与驱动程序交互函数DeviceIoControl详解

    这种通信方式,就是驱动程序和应用程序自定义一种IO控制码,然后调用DeviceIoControl函数,IO管理器会产生一个MajorFunction 为IRP_MJ_DEVICE_CONTROL(De ...

  7. QT(C++)DeviceIoControl()函数的相关使用

    Microsoft官网中有这个函数的介绍,对,仅仅就是介绍,有时候官网的查询结果也就只能看看-- 在我写过的一个软件中,我曾经多次使用该函数获取相关结果,现将我的使用经验分享给大家! DeviceIo ...

  8. Delphi DeviceIoControl函数

    转载于:https://www.cnblogs.com/fanweisheng/p/11390738.html

  9. Windows 内核数据结构学习总结

    <Windows内核编程>---基本数据结构 驱动对象: 每个驱动程序都会有唯一的驱动对象与之对应,并且这个驱动对象是在驱动加载时被内核中的对象管理程序所创建的.驱动对象用 DRIVER_ ...

最新文章

  1. tls 禁用重协商_TLS Https连接失败问题(协商失败)
  2. linux keepalived 脚本,Linux下 keepalived 的安装和配置
  3. spring aop聊点不一样的东西
  4. C语言函数 bzero
  5. matlab的gaot在哪里,最权威遗传算法工具箱GAOT(gaot)安装方法
  6. 自动量程万用表的实现原理_电子元器件用指针万用表、数字万用表、自动量程万用表测量原理图解大全教会您怎么看与测的呢?...
  7. 人人商城小程序消息服务器配置,如何设置小程序模板消息?
  8. elasticsearch小记之—— unmapped_type的使用
  9. 商业智能BI推动制造业智能化转型
  10. 微信图文排版指南-如何在图片上添加文字?
  11. 2015北邮计算机考研复试上机题解
  12. 亚马逊FBA箱子贴标有哪些要求
  13. 2020大数据面经整理
  14. Python基础06-数据结构
  15. IDEA启动卡在preparing workspace
  16. CTF网络安全大赛学习笔记1010
  17. 黑磷量子点/铂杂化介孔二氧化硅纳米颗粒/负载黑磷量子点红细胞膜纳米囊泡BPQD-EMNVs的应用
  18. 当STM32遇上RFID
  19. java应用的开发步骤_简述一下Java应用程序的开发步骤
  20. Luogu4711「化学」相对分子质量

热门文章

  1. 基于uniapp缓存写的搜索历史记录,清空历史记录,点击历史记录直接搜索
  2. 【读点论文】Unified Perceptual Parsing for Scene Understanding 整合学习数据集的关系,用图像级标注完成像素级的事
  3. element ui 图片加载失败_vue 3.x 中使用element-ui时, el-image图片加载失败!!
  4. fzu1922非主流
  5. overwrite java_java中,到底overwrite和override有什么区别?
  6. 语音识别common1(音素,三音素)
  7. 【热门主题:NBA球星争锋相对主题】
  8. c语言button用法,C# Button:按钮控件
  9. Charles抓包使用教程(window端)
  10. dell服务器安装出现 scanning for devices