CreateEvent,DeviceIoControl的用法

 

CreateEvent,DeviceIoControl的用法

2010-06-07 21:38:45|  分類: 默認分類 |  標籤: |字號 訂閱

1.HANDLE      CreateEvent(   
         LPSECURITY_ATTRIBUTES      lpEventAttributes,      //      SD   
         BOOL      bManualReset,                                                  //      reset      type   
         BOOL      bInitialState,                                                //      initial      state   
         LPCTSTR      lpName                                                          //      object      name   
     );   
     該函數創建一個Event同步對象,並返回該對象的Handle   
    
     lpEventAttributes      一般爲NULL   
     bManualReset                創建的Event是自動復位還是人工復位 ,如果true,人工復位,   
     一旦該Event被設置爲有信號,則它一直會等到ResetEvent()API被調用時纔會恢復爲無信號.      如果爲false,Event被設置爲有信號,則當有一個wait到它的Thread時,    該Event就會自動復位,變成無信號.   
     bInitialState              初始狀態,true,有信號,false無信號   
     lpName                            Event對象名   
    
       一個Event被創建以後,可以用OpenEvent()API來獲得它的Handle,用CloseHandle() 來關閉它,用SetEvent()或PulseEvent()來設置它使其有信號,用ResetEvent()來使其無信號,用WaitForSingleObject()或WaitForMultipleObjects()來等待其變爲有信號.   
        PulseEvent()是一個比較有意思的使用方法,正如這個API的名字,它使一個Event 對象的狀態發生一次脈衝變化,從無信號變成有信號再變成無信號,而整個操作是原子的.  對自動復位的Event對象,它僅釋放第一個等到該事件的thread(如果有),而對於人工復位的Event對象,它釋放所有等待的thread. 
By:http://hi.baidu.com/anthonywanted/blog/item/3dfaebef9c202334acafd5e7.html


 2.       應用程序和驅動程序的通信過程是:應用程序使用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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章