System.Runtime.InteropServices.ComTypes.IStream to System.IO.Stream

痴心易碎 提交于 2019-12-09 22:08:52

问题


In a .NET 3.0 project, I need to make some calls via P/Invoke (specifically to the PrintTicket Provider functions like PTConvertPrintTicketToDevMode() ).

The framework has a managed definition of the COM IStream interface: System.Runtime.InteropServices.ComTypes.IStream

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 System.IO.Stream 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.

Or am I going at this the wrong way?

I have PTConvertPrintTicketToDevMode() imported as:

    [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);

回答1:


It's not really straightforward (ie. there is no built in way to do it in the framework), but the idea is to implement a class that implements System.Runtime.InteropServices.ComTypes.IStream. Here is an article with the code. The article does not implement the Write method, but it should be another 10 minutes to write that. Check it out. http://www.sturmnet.org/blog/archives/2005/03/03/cds-csharp-extractor




回答2:


You shouldn't need to PInvoke, the PrintTicketConverter type in .Net already does this for you



来源:https://stackoverflow.com/questions/385684/system-runtime-interopservices-comtypes-istream-to-system-io-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!