Add 'Task<TOut?> DeviceIoControlAsync<TIn, TOut>(HFILE hDevice, uint ioControlCode, TIn? inVal)' for instead of 'DeviceIoControlAsync<TIn, TOut>(HFILE hDevice, uint ioControlCode, TIn? inVal, TOut? outVal)'.

pull/26/head
iFeng Huang 2018-12-14 23:09:48 +08:00
parent ca28272d0e
commit 6bf69058c6
1 changed files with 68 additions and 13 deletions

View File

@ -109,7 +109,7 @@ namespace Vanara.PInvoke
/// </list>
/// </remarks>
public static Task DeviceIoControlAsync<TIn>(HFILE hDev, uint ioControlCode, TIn inVal) where TIn : struct =>
DeviceIoControlAsync(hDev, ioControlCode, (TIn?)inVal, (int?)null);
DeviceIoControlAsync<TIn, int>(hDev, ioControlCode, inVal);
/// <summary>
/// Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.
@ -157,8 +157,7 @@ namespace Vanara.PInvoke
/// </remarks>
public static Task<TOut?> DeviceIoControlAsync<TOut>(HFILE hDev, uint ioControlCode) where TOut : struct
{
var outVal = default(TOut);
return DeviceIoControlAsync(hDev, ioControlCode, (int?)null, (TOut?)outVal);
return DeviceIoControlAsync<int, TOut>(hDev, ioControlCode, null);
}
/// <summary>
@ -214,22 +213,78 @@ namespace Vanara.PInvoke
/// </item>
/// </list>
/// </remarks>
[Obsolete("Use 'Task<TOut?> DeviceIoControlAsync<TIn, TOut>(HFILE hDevice, uint ioControlCode, TIn? inVal)' instead.")]
public static Task<TOut?> DeviceIoControlAsync<TIn, TOut>(HFILE hDevice, uint ioControlCode, TIn? inVal, TOut? outVal) where TIn : struct where TOut : struct
{
var buf = Pack(inVal, outVal);
return new TaskFactory().FromAsync(BeginDeviceIoControl<TIn, TOut>, EndDeviceIoControl<TIn, TOut>, hDevice, ioControlCode, buf, null);
}
/// <summary>Explicits the device io control asynchronous.</summary>
/// <typeparam name="TIn">The type of the in.</typeparam>
/// <typeparam name="TOut">The type of the out.</typeparam>
/// <param name="hDevice">The h device.</param>
/// <param name="ioControlCode">The io control code.</param>
/// <param name="inVal">The in value.</param>
/// <param name="outVal">The out value.</param>
/// <returns></returns>
/// <exception cref="Win32Exception"></exception>
private static unsafe Task<TOut?> ExplicitDeviceIoControlAsync<TIn, TOut>(HFILE hDevice, uint ioControlCode, TIn? inVal, TOut? outVal) where TIn : struct where TOut : struct
/// <summary>
/// Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.
/// </summary>
/// <typeparam name="TIn">The type of the <paramref name="inVal"/>.</typeparam>
/// <typeparam name="TOut">The type of the return value.</typeparam>
/// <param name="hDevice">
/// 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 function. For more information, see Remarks.
/// </param>
/// <param name="ioControlCode">
/// 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.
/// </param>
/// <param name="inVal">
/// The input value required to perform the operation. The type of this data depends on the value of the <paramref
/// name="ioControlCode"/> parameter.
/// </param>
/// <returns>An asynchronous result containing the populated data supplied by <typeparamref name="TOut"/>.</returns>
/// <remarks>
/// <para>
/// To retrieve a handle to the device, you must call the CreateFile function 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:
/// </para>
/// <para>\\.\DeviceName</para>
/// <para>
/// 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.
/// </para>
/// <para>
/// 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 other
/// CreateFile parameters as follows when opening a device handle:
/// </para>
/// <list type="bullet">
/// <item>
/// <description>The fdwCreate parameter must specify OPEN_EXISTING.</description>
/// </item>
/// <item>
/// <description>The hTemplateFile parameter must be NULL.</description>
/// </item>
/// <item>
/// <description>
/// The fdwAttrsAndFlags parameter can specify FILE_FLAG_OVERLAPPED to indicate that the returned handle can be used in overlapped
/// (asynchronous) I/O operations.
/// </description>
/// </item>
/// </list>
/// </remarks>
public static Task<TOut?> DeviceIoControlAsync<TIn, TOut>(HFILE hDevice, uint ioControlCode, TIn? inVal) where TIn : struct where TOut : struct {
TOut? outValue = default(TOut);
var buf = Pack(inVal, outValue);
return Task.Factory.FromAsync(BeginDeviceIoControl<TIn, TOut>, EndDeviceIoControl<TIn, TOut>, hDevice, ioControlCode, buf, null);
}
/// <summary>Explicits the device io control asynchronous.</summary>
/// <typeparam name="TIn">The type of the in.</typeparam>
/// <typeparam name="TOut">The type of the out.</typeparam>
/// <param name="hDevice">The h device.</param>
/// <param name="ioControlCode">The io control code.</param>
/// <param name="inVal">The in value.</param>
/// <param name="outVal">The out value.</param>
/// <returns></returns>
/// <exception cref="Win32Exception"></exception>
private static unsafe Task<TOut?> ExplicitDeviceIoControlAsync<TIn, TOut>(HFILE hDevice, uint ioControlCode, TIn? inVal, TOut? outVal) where TIn : struct where TOut : struct
{
#pragma warning disable CS0618 // Type or member is obsolete
ThreadPool.BindHandle((IntPtr)hDevice);