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

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

Request for XpsPrint.dll

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



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.
Code:

#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
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6641

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.
Thanks!
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