日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DeviceIoControl的使用说明

發布時間:2025/5/22 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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, //沒有返回的數據,置為NULL0, //沒有返回的數據,置為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, //沒有向設備傳遞的數據,置為NULL0, //沒有向設備傳遞的數據,置為NULLbTemp, //讀取設備的數據返回地址iSize, //讀取數據的字節數&nOutput,NULL)) {return false; } //放置到公用數組 memcpy(&bData[0],&bTemp[0],iSize); return true; }

總結

以上是生活随笔為你收集整理的DeviceIoControl的使用说明的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。