WritePrinter (winspool)
Last changed: -

The WritePrinter function notifies the print spooler that data should be written to the specified printer.

C# Signature:

[DllImport("winspool.drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, UInt32 dwCount, out UInt32 dwWritten);

VB Signature:

    <DllImport("winspool.Drv", EntryPoint:="WritePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal pBytes As IntPtr, ByVal dwCount As Int32, ByRef dwWritten As Int32) As Boolean
    End Function

VB 2008 Signature:

<DllImport("winspool.drv")> Private Function WritePrinter(ByVal hPrinter As IntPtr, ByVal pBytes As IntPtr, ByVal dwCount As UInt32, ByVal dwWritten As UInt32) As Boolean

User-Defined Types:


Alternative Managed API:

Do you know one? Please contribute it!



Tips & Tricks:

Please add some!

Sample Code:

    // From: http://support.microsoft.com/kb/322091
    // SendBytesToPrinter()
    // When the function is given a printer name and an unmanaged array
    // of bytes, the function sends those bytes to the print queue.
    // Returns true on success, false on failure.
    public static bool SendBytesToPrinter( string szPrinterName, IntPtr pBytes, Int32 dwCount)
    Int32    dwError = 0, dwWritten = 0;
    IntPtr    hPrinter = new IntPtr(0);
    DOCINFOA    di = new DOCINFOA();
    bool    bSuccess = false; // Assume failure unless you specifically succeed.

    di.pDocName = "My C#.NET RAW Document";
    di.pDataType = "RAW";

    // Open the printer.
    if( OpenPrinter( szPrinterName.Normalize(), out hPrinter, IntPtr.Zero ) )
        // Start a document.
        if( StartDocPrinter(hPrinter, 1, di) )
        // Start a page.
        if( StartPagePrinter(hPrinter) )
            // Write your bytes.
            bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
    // If you did not succeed, GetLastError may give more information
    // about why not.
    if( bSuccess == false )
        dwError = Marshal.GetLastWin32Error();
    return bSuccess;