P/调用可选的out指针
本文关键字:out 指针 调用 | 更新日期: 2023-09-27 18:15:10
我如何调用像StartXpsPrintJob这样的方法,它可选地返回一个指向COM对象的指针,这取决于是否为OUT参数传递NULL。
c++中的例子是printTicketStream
set NULL
:
IXpsPrintJob *job = NULL; IXpsPrintJobStream *jobStream = NULL; hr = StartXpsPrintJob( printerName, NULL, NULL, NULL, completionEvent, NULL, 0, &job, &jobStream, /* _Out_ IXpsPrintJobStream **printTicketStream */ NULL);
我知道我可以使可选参数IntPtr
而不是out IntPtr
,并且只传递IntPtr.Zero
,但这将需要该函数的四个p/Invoke签名来包含所有选项,更不用说调用代码看起来像什么了。
由于形参实际上是指向保存结果的指针的指针,因此分配一个指针并将其作为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