System.IO.Pipes.PipeStream.WriteFileNative C# (CSharp) Méthode

WriteFileNative() private méthode

private WriteFileNative ( SafePipeHandle handle, byte buffer, int offset, int count, NativeOverlapped overlapped, int &errorCode ) : int
handle Microsoft.Win32.SafeHandles.SafePipeHandle
buffer byte
offset int
count int
overlapped System.Threading.NativeOverlapped
errorCode int
Résultat int
        private unsafe int WriteFileNative(SafePipeHandle handle, byte[] buffer, int offset, int count,
                NativeOverlapped* overlapped, out int errorCode)
        {
            DebugAssertReadWriteArgs(buffer, offset, count, handle);
            Debug.Assert((_isAsync && overlapped != null) || (!_isAsync && overlapped == null), "Async IO parameter screwup in call to WriteFileNative.");

            // You can't use the fixed statement on an array of length 0. Note that async callers
            // check to avoid calling this first, so they can call user's callback
            if (buffer.Length == 0)
            {
                errorCode = 0;
                return 0;
            }

            int numBytesWritten = 0;
            int r = 0;

            fixed (byte* p = buffer)
            {
                if (_isAsync)
                {
                    r = Interop.Kernel32.WriteFile(handle, p + offset, count, IntPtr.Zero, overlapped);
                }
                else
                {
                    r = Interop.Kernel32.WriteFile(handle, p + offset, count, out numBytesWritten, IntPtr.Zero);
                }
            }

            if (r == 0)
            {
                errorCode = Marshal.GetLastWin32Error();
                return -1;
            }
            else
            {
                errorCode = 0;
            }

            return numBytesWritten;
        }