由于参数实际上是指向将保存结果的指针的指针,因此分配一个指针并将其作为IntPtr
.
示例签名:
[DllImport("XpsPrint.dll", EntryPoint = "StartXpsPrintJob", PreserveSig = false)]
private static extern void StartXpsPrintJob(
// other parameters omitted for brevity
IntPtr printTicketStream);
示例使用:
IXpsPrintJobStream ticketStream = null;
bool getTicketStream = false;
IntPtr ppTicketStream = IntPtr.Zero;
if (getTicketStream)
{
ppTicketStream = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(IntPtr)));
Marshal.StructureToPtr(IntPtr.Zero, ppTicketStream, false);
}
StartXpsPrintJob(ppTicketStream);
if (getTicketStream)
{
IntPtr pTicketStream = (IntPtr)Marshal.PtrToStructure(ppTicketStream, typeof(IntPtr));
ticketStream = (IXpsPrintJobStream)Marshal.GetTypedObjectForIUnknown(pTicketStream, typeof(IXpsPrintJobStream));
Marshal.FreeCoTaskMem(ppTicketStream);
}
//ticket stream now has the result from StartXpsPrintJob or null, if it was not requested