diff --git a/PInvoke/Shared/OverlappedAsyncBase.cs b/PInvoke/Shared/OverlappedAsyncBase.cs index 5f960ecb..f1c82742 100644 --- a/PInvoke/Shared/OverlappedAsyncBase.cs +++ b/PInvoke/Shared/OverlappedAsyncBase.cs @@ -85,6 +85,28 @@ namespace Vanara.PInvoke return ar; } + /// Setups the class that holds the overlapped call details. Use at the start of the 'Begin' method. + /// The file to bind to. + /// The user callback method. + /// + /// A variable that is passed to all receiving methods in the asynchronous methods. Can be use to hold any user defined object. + /// + /// An instance for the asynchronous calls. + public static unsafe OverlappedAsyncResult SetupOverlappedFunction(System.IO.FileStream fileStream, AsyncCallback userCallback, object userState) + { + var ar = new OverlappedAsyncResult(userState, userCallback, fileStream.SafeFileHandle.DangerousGetHandle()); + var o = new Overlapped(0, 0, IntPtr.Zero, ar); + ar.Overlapped = o.Pack((code, bytes, pOverlapped) => + { + var asyncResult = (OverlappedAsyncResult)Overlapped.Unpack(pOverlapped).AsyncResult; + if (asyncResult.IsCompleted) return; + asyncResult.FreeOverlapped(); + if (code == 0x217) code = 0; + asyncResult.Complete(true, (int)code); + }, userState); + return ar; + } + private static void BindHandle(HFILE hDevice) { if (boundHandles.Add(hDevice))