标题：DeviceIoControl
基本信息
DeviceIoControl 
VB声明Declare Function DeviceIoControl Lib "kernel32" Alias "DeviceIoControl" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As OVERLAPPED) As Long 
说明对设备执行指定的操作 
返回值 
Long，非零表示成功，零表示失败。会设置GetLastError 
参数表 
参数 类型及说明
hDevice Long，设备句柄 
dwIoControlCode Long，应用程序调用驱动程序的控制命令，就是IOCTL_XXX IOCTLs。 
lpInBuffer Any，应用程序传递给驱动程序的数据缓冲区地址。
nInBufferSize Long，应用程序传递给驱动程序的数据缓冲区大小，字节数。 
lpOutBuffer Any，驱动程序返回给应用程序的数据缓冲区地址。 
nOutBufferSize Long，驱动程序返回给应用程序的数据缓冲区大小，字节数。 
lpBytesReturned Long，驱动程序实际返回给应用程序的数据字节数地址。 
lpOverlapped OVERLAPPED，这个结构用于重叠操作。针对同步操作，请用ByVal As Long传递零值 
注解可用于windows 95 和 windows nt，但并非所有的操作都得到了两种操作系统的同时支持
vc下的DeviceIoControl 函数定义
DeviceIoControl Function 　Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.
语法说明
SyntaxBOOL WINAPI DeviceIoControl( __in HANDLE hDevice, __in DWORD dwIoControlCode, __in_opt LPVOID lpInBuffer, __in DWORD nInBufferSize, __out_opt LPVOID lpOutBuffer, __in DWORD nOutBufferSize, __out_opt LPDWORD lpBytesReturned, __inout_opt LPOVERLAPPED lpOverlapped);
ParametershDevice [in]A handle to the device on which the operation is to be performed. The device is typically a volume, directory, file, or stream. To retrieve a device handle, use the CreateFilefunction. For more information, see Remarks. 　dwIoControlCode [in]The control code for the operation. This value identifies the specific operation to be performed and the type of device on which to perform it. 　For a list of the control codes, see Remarks. The documentation for each control code provides usage details for the lpInBuffer,nInBufferSize, lpOutBuffer, and nOutBufferSize parameters. 　lpInBuffer [in, optional]A pointer to the input buffer that contains the data required to perform the operation. The format of this data depends on the value of the dwIoControlCode parameter. 　This parameter can be NULL if dwIoControlCode specifies an operation that does not require input data. 　nInBufferSize [in]The size of the input buffer, in bytes. 　lpOutBuffer [out, optional]A pointer to the output buffer that is to receive the data returned by the operation. The format of this data depends on the value of the dwIoControlCode parameter. 　This parameter can be NULL if dwIoControlCode specifies an operation that does not return data. 　nOutBufferSize [in]The size of the output buffer, in bytes. 　lpBytesReturned [out, optional]A pointer to a variable that receives the size of the data stored in the output buffer, in bytes. 　If the output buffer is too small to receive any data, the call fails, GetLastError returns ERROR_INSUFFICIENT_BUFFER, and lpBytesReturned is zero. 　If the output buffer is too small to hold all of the data but can hold some entries, some drivers will return as much data as fits. In this case, the call fails, GetLastError returns ERROR_MORE_DATA, and lpBytesReturned indicates the amount of data received. Your application should callDeviceIoControl again with the same operation, specifying a new starting point. 　If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an operation returns no output data and lpOutBuffer is NULL, DeviceIoControl makes use of lpBytesReturned. After such an operation, the value of lpBytesReturned is meaningless. 　If lpOverlapped is not NULL, lpBytesReturned can be NULL. If this parameter is not NULL and the operation returns data,lpBytesReturned is meaningless until the overlapped operation has completed. To retrieve the number of bytes returned, callGetOverlappedResult. If hDevice is associated with an I/O completion port, you can retrieve the number of bytes returned by calling GetQueuedCompletionStatus. 　lpOverlapped [in, out, optional]A pointer to an OVERLAPPED structure. 　If hDevice was opened without specifying FILE_FLAG_OVERLAPPED, lpOverlapped is ignored. 　If hDevice was opened with the FILE_FLAG_OVERLAPPED flag, the operation is performed as an overlapped (asynchronous) operation. In this case, lpOverlapped must point to a validOVERLAPPED structure that contains a handle to an event object. Otherwise, the function fails in unpredictable ways. 　For overlapped operations, DeviceIoControl returns immediately, and the event object is signaled when the operation has been completed. Otherwise, the function does not return until the operation has been completed or an error occurs.
Return ValueIf the operation completes successfully, the return value is nonzero. 　If the operation fails or is pending, the return value is zero. To get extended error information, call GetLastError.
RemarksTo retrieve a handle to the device, you must call the CreateFilefunction with either the name of a device or the name of the driver associated with a device. To specify a device name, use the following format: 　\\.\DeviceName 　DeviceIoControl can accept a handle to a specific device. For example, to open a handle to the logical drive A: with CreateFile, specify \\.\a:. Alternatively, you can use the names \\.\PhysicalDrive0, \\.\PhysicalDrive1, and so on, to open handles to the physical drives on a system. 　You should specify the FILE_SHARE_READ and FILE_SHARE_WRITE access flags when calling CreateFile to open a handle to a device driver. However, when you open a communications resource, such as a serial port, you must specify exclusive access. Use the otherCreateFile parameters as follows when opening a device handle: 　The fdwCreate parameter must specify OPEN_EXISTING.
The hTemplateFile parameter must be NULL.
The fdwAttrsAndFlags parameter can specify FILE_FLAG_OVERLAPPED to indicate that the returned handle can be used in overlapped (asynchronous) I/O operations.
Example
从设备读取数据 
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;
}