title: System.Runtime.InteropServices.ComTypes.IStream to System.IO.Stream
body: <p>In a .NET 3.0 project, I need to make some calls via P/Invoke
  (specifically to the PrintTicket Provider functions like PTConvertPrintTicketToDevMode() ).</p>
  
  <p>The framework has a managed definition of the COM IStream interface:
  <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comtypes.istream_members.aspx" rel="nofollow noreferrer" title="System.Runtime.InteropServices.ComTypes.IStream">System.Runtime.InteropServices.ComTypes.IStream</a></p>
  
  <p>I need this as System.IO.Stream so that I can easily use .NET classes (like XmlDocument) with the output of these imported functions. But I can't see a less painful way of converting this back and forth to <a href="http://msdn.microsoft.com/en-us/library/system.io.stream_members.aspx" rel="nofollow noreferrer" title="System.IO.Stream">System.IO.Stream</a> other than reading and writing data a block of bytes at a time. It seems too kludgey for a task that would seem very common.</p>
  
  <p>Or am I going at this the wrong way?</p>
  
  <p>I have <a href="http://msdn.microsoft.com/en-us/library/ms536106(VS.85).aspx" rel="nofollow noreferrer" title="PTConvertPrintTicketToDevMode">PTConvertPrintTicketToDevMode</a>() imported as:</p>
  
  <pre><code>    [DllImport("prntvpt.dll")]
      public static extern int 
      PTConvertPrintTicketToDevMode(IntPtr hProvider, 
                                    IStream pPrintTicket,
                                    EDefaultDevmodeType baseDevmodeType,
                                    EPrintTicketScope scope,
                                    IntPtr pcbDevmode,
                                    out IntPtr ppDevmode,
                                    [MarshalAs(UnmanagedType.BStr)]out String pbstrErrorMessage);
  </code></pre>