DeviceIoControl的使用說明

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.