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