Red Gate forums :: View topic - Request for XpsPrint.dll
Return to RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
PInvoke.Net forum

Request for XpsPrint.dll

Search in PInvoke.Net forum
Post new topic   Reply to topic
Jump to:  
Author Message

Joined: 25 May 2013
Posts: 1

PostPosted: Sat May 25, 2013 1:06 am    Post subject: Request for XpsPrint.dll Reply with quote

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.

#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)
        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();

        // Preservesig is set, so this will throw on failure
        StartXpsPrintJob(printerName, jobName, outputFileName,
            pProgressEvent, pCompleteEvent,
            pagesToPrintB, cpagesToPrintB,
            ppPrintJob, out documentStream, ppTicketStream);
        if (pProgressEvent != IntPtr.Zero)
        if (pCompleteEvent != IntPtr.Zero)

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

    if (getTicketStream)
        IntPtr pTicketStream = (IntPtr)Marshal.PtrToStructure(ppTicketStream, typeof(IntPtr));
        ticketStream = (IXpsPrintJobStream)Marshal.GetTypedObjectForIUnknown(pTicketStream, typeof(IXpsPrintJobStream));
        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);


#region Structures

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

internal interface IXpsPrintJob
    void Cancel();
    void GetJobStatus(out XPS_JOB_STATUS jobStatus);

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

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

Back to top
View user's profile Send private message
Brian Donahue

Joined: 23 Aug 2004
Posts: 6678

PostPosted: Tue May 28, 2013 10:55 am    Post subject: Reply with quote

xpsprint module should be available in PInvoke wiki for you to use now.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group