Esempio n. 1
0
        public unsafe static void ReadFromStream(FileStream input, void *pvBuffer, uint length)
        {
            SafeFileHandle sfhFile = input.SafeFileHandle;

            if (sfhFile.IsInvalid)
            {
                throw new ArgumentException("input", "File is closed");
            }

            void *pvRead = pvBuffer;

            while (length > 0)
            {
                uint read;
                bool result = SafeNativeMethods.ReadFile(sfhFile, pvRead, length, out read, IntPtr.Zero);

                if (!result)
                {
                    NativeMethods.ThrowOnWin32Error("ReadFile() returned false");
                }

                if (result && read == 0)
                {
                    throw new EndOfStreamException();
                }

                pvRead  = (void *)((byte *)pvRead + read);
                length -= read;
            }

            GC.KeepAlive(input);
        }