System.Runtime.InteropServices.ComTypes.IStream to

2020-07-25 08:57发布

问题:

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