2 // System.Drawing.ComIStreamWrapper.cs
5 // Kornél Pál <http://www.kornelpal.hu/>
7 // Copyright (C) 2005 Kornél Pál
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Reflection;
34 using System.Runtime.InteropServices;
36 using System.Runtime.InteropServices.ComTypes;
37 using STATSTG = System.Runtime.InteropServices.ComTypes.STATSTG;
39 using IStream = System.Runtime.InteropServices.UCOMIStream;
42 namespace System.Drawing
44 // Stream to IStream wrapper for COM interop
45 internal sealed class ComIStreamWrapper : IStream
47 private const int STG_E_INVALIDFUNCTION = unchecked((int)0x80030001);
49 private readonly Stream baseStream;
50 private long position = -1;
52 internal ComIStreamWrapper(Stream stream)
57 private void SetSizeToPosition()
61 if (position > baseStream.Length)
62 baseStream.SetLength(position);
63 baseStream.Position = position;
68 public void Read(byte[] pv, int cb, IntPtr pcbRead)
76 read = baseStream.Read(pv, 0, cb);
79 if (pcbRead != IntPtr.Zero)
80 Marshal.WriteInt32(pcbRead, read);
83 public void Write(byte[] pv, int cb, IntPtr pcbWritten)
89 baseStream.Write(pv, 0, cb);
92 if (pcbWritten != IntPtr.Zero)
93 Marshal.WriteInt32(pcbWritten, cb);
96 public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
100 if (baseStream.CanWrite)
102 switch ((SeekOrigin)dwOrigin)
104 case SeekOrigin.Begin:
105 newPosition = dlibMove;
107 case SeekOrigin.Current:
108 if ((newPosition = position) == -1)
109 newPosition = baseStream.Position;
110 newPosition += dlibMove;
113 newPosition = baseStream.Length + dlibMove;
116 throw new ExternalException(null, STG_E_INVALIDFUNCTION);
119 if (newPosition > baseStream.Length)
120 position = newPosition;
123 baseStream.Position = newPosition;
131 newPosition = baseStream.Seek(dlibMove, (SeekOrigin)dwOrigin);
133 catch (ArgumentException)
135 throw new ExternalException(null, STG_E_INVALIDFUNCTION);
140 if (plibNewPosition != IntPtr.Zero)
141 Marshal.WriteInt64(plibNewPosition, newPosition);
144 public void SetSize(long libNewSize)
146 baseStream.SetLength(libNewSize);
149 public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
151 byte[] buffer = new byte[4096];
162 if (written + 4096 > cb)
163 count = (int)(cb - written);
165 if ((read = baseStream.Read(buffer, 0, count)) == 0)
167 pstm.Write(buffer, read, IntPtr.Zero);
169 } while (written < cb);
172 if (pcbRead != IntPtr.Zero)
173 Marshal.WriteInt64(pcbRead, written);
174 if (pcbWritten != IntPtr.Zero)
175 Marshal.WriteInt64(pcbWritten, written);
178 public void Commit(int grfCommitFlags)
185 throw new ExternalException(null, STG_E_INVALIDFUNCTION);
188 public void LockRegion(long libOffset, long cb, int dwLockType)
190 throw new ExternalException(null, STG_E_INVALIDFUNCTION);
193 public void UnlockRegion(long libOffset, long cb, int dwLockType)
195 throw new ExternalException(null, STG_E_INVALIDFUNCTION);
198 public void Stat(out STATSTG pstatstg, int grfStatFlag)
200 pstatstg = new STATSTG();
201 pstatstg.cbSize = baseStream.Length;
204 public void Clone(out IStream ppstm)
207 throw new ExternalException(null, STG_E_INVALIDFUNCTION);