應用程序和驅動程序的通信過程是:應用程序使用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; }