Request for XpsPrint.dll

Announcements and discussion for PInvoke

Moderators: Luke Jefferson, Robert

Request for XpsPrint.dll

Postby mgaffigan » Sat May 25, 2013 1:06 am

I have a signature for XpsPrint.dll - StartXpsPrintJob, but nowhere to put it. I was hoping someone could create the new module. Thank you again for the great service provided to the .Net community.

I will post the signature below until the topic is created.
Code: Select all
#region P/Invokes

public static void StartXpsPrintJobSafe(
    string printerName, string jobName, string outputFileName,
    SafeWaitHandle progressEvent, SafeWaitHandle completeEvent,
    bool[] pagesToPrint,
    bool getPrintJob, out IXpsPrintJob printJob,
    out IXpsPrintJobStream documentStream,
    bool getTicketStream, out IXpsPrintJobStream ticketStream)
{
    if (string.IsNullOrWhiteSpace(printerName))
        throw new ArgumentNullException(\"printerName\");

    byte[] pagesToPrintB = null;
    UInt32 cpagesToPrintB = 0;
    if (pagesToPrint != null && pagesToPrint.Length > 0)
    {
        pagesToPrintB = pagesToPrint
            .Select(b => b ? (byte)1 : (byte)0)
            .ToArray();
        cpagesToPrintB = (UInt32)pagesToPrintB.Length;
    }

    IntPtr ppPrintJob = IntPtr.Zero;
    IntPtr ppTicketStream = IntPtr.Zero;
    // memory is allocated by StartXpsPrintJob, we just specify we want something
    if (getPrintJob)
    {
        ppPrintJob = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(IntPtr)));
        Marshal.StructureToPtr(IntPtr.Zero, ppPrintJob, false);
    }
    if (getTicketStream)
    {
        ppTicketStream = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(IntPtr)));
        Marshal.StructureToPtr(IntPtr.Zero, ppTicketStream, false);
    }

    // take references on safehandles
    IntPtr pProgressEvent = IntPtr.Zero;
    IntPtr pCompleteEvent = IntPtr.Zero;

    if (progressEvent != null)
    {
        bool success = false;
        progressEvent.DangerousAddRef(ref success);
        if (!success)
            throw new ObjectDisposedException(\"progressEvent\");
        pProgressEvent = progressEvent.DangerousGetHandle();
    }
    if (completeEvent != null)
    {
        bool success = false;
        completeEvent.DangerousAddRef(ref success);
        if (!success)
            throw new ObjectDisposedException(\"completeEvent\");
        pCompleteEvent = completeEvent.DangerousGetHandle();
    }

    RuntimeHelpers.PrepareConstrainedRegions();
    try
    {
        // Preservesig is set, so this will throw on failure
        StartXpsPrintJob(printerName, jobName, outputFileName,
            pProgressEvent, pCompleteEvent,
            pagesToPrintB, cpagesToPrintB,
            ppPrintJob, out documentStream, ppTicketStream);
    }
    finally
    {
        if (pProgressEvent != IntPtr.Zero)
            progressEvent.DangerousRelease();
        if (pCompleteEvent != IntPtr.Zero)
            completeEvent.DangerousRelease();
    }

    if (getPrintJob)
    {
        IntPtr pPrintJob = (IntPtr)Marshal.PtrToStructure(ppPrintJob, typeof(IntPtr));
        printJob = (IXpsPrintJob)Marshal.GetTypedObjectForIUnknown(pPrintJob, typeof(IXpsPrintJob));
        Marshal.FreeCoTaskMem(ppPrintJob);
    }
    else
    {
        printJob = null;
    }

    if (getTicketStream)
    {
        IntPtr pTicketStream = (IntPtr)Marshal.PtrToStructure(ppTicketStream, typeof(IntPtr));
        ticketStream = (IXpsPrintJobStream)Marshal.GetTypedObjectForIUnknown(pTicketStream, typeof(IXpsPrintJobStream));
        Marshal.FreeCoTaskMem(ppTicketStream);
    }
    else
    {
        ticketStream = null;
    }
}

[DllImport(\"XpsPrint.dll\", EntryPoint = \"StartXpsPrintJob\", PreserveSig = false)]
private static extern void StartXpsPrintJob(
    // 0
    [MarshalAs(UnmanagedType.LPWStr)] String printerName,
    [MarshalAs(UnmanagedType.LPWStr)] String jobName,
    // 2
    [MarshalAs(UnmanagedType.LPWStr)] String outputFileName,
    IntPtr progressEvent,   // HANDLE
    // 4
    IntPtr completionEvent, // HANDLE
    [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 6)] byte[] printablePagesOn,
    // 6
    UInt32 printablePagesOnCount,
    IntPtr xpsPrintJob,
    // 8
    out IXpsPrintJobStream documentStream,
    IntPtr printTicketStream);

#endregion

#region Structures

[Guid(\"7a77dc5f-45d6-4dff-9307-d8cb846347ca\")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IXpsPrintJobStream
{
    void Read([MarshalAs(UnmanagedType.LPArray)] byte[] pv, uint cb, out uint pcbRead);
    void Write([MarshalAs(UnmanagedType.LPArray)] byte[] pv, uint cb, out uint pcbWritten);
    void Close();
}

[Guid(\"5ab89b06-8194-425f-ab3b-d7a96e350161\")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IXpsPrintJob
{
    void Cancel();
    void GetJobStatus(out XPS_JOB_STATUS jobStatus);
}

[StructLayout(LayoutKind.Sequential)]
internal struct XPS_JOB_STATUS
{
    public UInt32 jobId;
    public Int32 currentDocument;
    public Int32 currentPage;
    public Int32 currentPageTotal;
    public XPS_JOB_COMPLETION completion;
    public Int32 jobStatus;
};

internal enum XPS_JOB_COMPLETION
{
    XPS_JOB_IN_PROGRESS = 0,
    XPS_JOB_COMPLETED = 1,
    XPS_JOB_CANCELLED = 2,
    XPS_JOB_FAILED = 3
}

internal enum WAIT_RESULT
{
    WAIT_OBJECT_0 = 0,
    WAIT_ABANDONED = 0x80,
    WAIT_TIMEOUT = 0x102,
    WAIT_FAILED = -1
}

#endregion
mgaffigan
 
Posts: 1
Joined: Sat May 25, 2013 12:58 am

Postby Brian Donahue » Tue May 28, 2013 10:55 am

xpsprint module should be available in PInvoke wiki for you to use now.
Thanks!
Brian Donahue
 
Posts: 6668
Joined: Mon Aug 23, 2004 10:48 am


Return to PInvoke.Net

Who is online

Users browsing this forum: No registered users and 0 guests