Kernel32DeviceIoControl(IntPtr, UInt32, IntPtr, Int32, IntPtr, Int32, Int32, IntPtr) Method

Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.

Definition

Namespace: DevCase.Win32.NativeMethods
Assembly: DevCase.net48 (in DevCase.net48.dll) Version: 6.0.0.0 (6.0)
XMLNS for XAML: Not mapped to an xmlns.
[DllImportAttribute("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static bool DeviceIoControl(
	IntPtr hDevice,
	uint ioControlCode,
	[OptionalAttribute] IntPtr inputBuffer,
	[OptionalAttribute] int inputBufferSize,
	[OptionalAttribute] IntPtr outputBuffer,
	[OptionalAttribute] int outputBufferSize,
	out int refBytesReturned,
	[OptionalAttribute] IntPtr overlapped
)

Parameters

hDevice  IntPtr
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 CreateFile(String, FileAccessRights, FileShare, IntPtr, FileMode, CreateFileFlags, IntPtr) function.

ioControlCode  UInt32
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.

inputBuffer  IntPtr  (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 ioControlCode parameter.

This parameter can be if ioControlCode parameter specifies an operation that does not require input data.

inputBufferSize  Int32  (Optional)
The size of the input buffer specified in the inputBuffer parameter, in bytes.
outputBuffer  IntPtr  (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 if ioControlCode parameter specifies an operation that does not return data.

outputBufferSize  Int32  (Optional)
The size of the input buffer specified in the outputBuffer parameter, in bytes.
refBytesReturned  Int32
A pointer to a variable that receives the size of the data stored in the output buffer of outputBuffer parameter, in bytes.

If the output buffer is too small to receive any data, the call fails, GetLastWin32Error returns ERROR_INSUFFICIENT_BUFFER, and refBytesReturned 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, GetLastWin32Error returns ERROR_MORE_DATA, and refBytesReturned indicates the amount of data received. Your application should call DeviceIoControl(SafeFileHandle, DirectoryManagementControlCodes, IntPtr, Int32, IntPtr, Int32, Int32, IntPtr) again with the same operation, specifying a new starting point.

If overlapped is , refBytesReturned cannot be . Even when an operation returns no output data and outputBuffer is , DeviceIoControl(SafeFileHandle, DirectoryManagementControlCodes, IntPtr, Int32, IntPtr, Int32, Int32, IntPtr) makes use of refBytesReturned. After such an operation, the value of refBytesReturned is meaningless.

If overlapped is not , refBytesReturned can be . If this parameter is not and the operation returns data, refBytesReturned is meaningless until the overlapped operation has completed. To retrieve the number of bytes returned, call GetOverlappedResult function.

If hDevice is associated with an I/O completion port, you can retrieve the number of bytes returned by calling GetQueuedCompletionStatus function.

overlapped  IntPtr  (Optional)
A pointer to an OVERLAPPED structure.

If hDevice was opened without specifying Overlapped flag, overlapped parameter is ignored.

If hDevice was opened with the Overlapped flag, the operation is performed as an overlapped (asynchronous) operation. In this case, overlapped parameter must point to a valid OVERLAPPED structure that contains a handle to an event object. Otherwise, the function fails in unpredictable ways.

For overlapped operations, DeviceIoControl(SafeFileHandle, DirectoryManagementControlCodes, IntPtr, Int32, IntPtr, Int32, Int32, IntPtr) 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 Value

Boolean
If the operation completes successfully, the return value is .

If the operation fails or is pending, the return value is .

To get extended error information, call GetLastWin32Error.

Remarks

See Also