Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
tools
vsnet.act
vsnet.debugging
vsnet.documentation
vsnet.enterprise.tools
vsnet.general
vsnet.ide
vsnet.jlca
vsnet.servicepacks
vsnet.setup
vsnet.vsip
vsnet.vss
vsnet.vstools.office
vstudio.development
vstudio.extensibility
vstudio.general
vstudio.helpauthoring
vstudio.setup
vstudio.sourcesafe
  
 
date: Mon, 18 Aug 2008 11:53:13 -0500,    group: microsoft.public.vstudio.development        back       


How to print a spool file.   
I have large number of spool files that I need to send to a printer in my 
.net 2 C# code.

I am trying to write a load testing app that can take these spool files and 
print them over and over.

What's the best way of doing this print?

Thanks

Ertan Zanagar
date: Mon, 18 Aug 2008 11:53:13 -0500   author:   Ertan Zanagar

Re: How to print a spool file.   
And this is how you do it...




using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace ACR.LoadTestHarness.Classes
{
    public class RawPrinterHelper
    {
        private string documentName = "";
        // Structure and API declarions:
        [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError 
= true, CharSet = CharSet.Ansi,
            ExactSpelling = true, CallingConvention = 
CallingConvention.StdCall)]
        public static extern bool 
OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr 
hPrinter,
                                              IntPtr pd);
        [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError 
= true, ExactSpelling = true,
            CallingConvention = CallingConvention.StdCall)]
        public static extern bool ClosePrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", 
SetLastError = true, CharSet = CharSet.Ansi,
            ExactSpelling = true, CallingConvention = 
CallingConvention.StdCall)]
        public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 
level,
                                                  [In, 
MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

        [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", 
SetLastError = true, ExactSpelling = true,
            CallingConvention = CallingConvention.StdCall)]
        public static extern bool EndDocPrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", 
SetLastError = true, ExactSpelling = true,
            CallingConvention = CallingConvention.StdCall)]
        public static extern bool StartPagePrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", 
SetLastError = true, ExactSpelling = true,
            CallingConvention = CallingConvention.StdCall)]
        public static extern bool EndPagePrinter(IntPtr hPrinter);

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

        // 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 bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, 
Int32 dwCount)
        {
            IntPtr hPrinter;
            DOCINFOA di = new DOCINFOA();
            bool bSuccess = false; // Assume failure unless you specifically 
succeed.

            //di.pDocName = "My C#.NET RAW Document";
            di.pDataType = string.Format("{0} - {1}", 
ACR.LoadTestHarness.Properties.Resources.AppFriendlyName, documentName);

            // 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.
                        Int32 dwWritten = 0;
                        bSuccess = WritePrinter(hPrinter, pBytes, dwCount, 
out dwWritten);
                        EndPagePrinter(hPrinter);
                    }
                    EndDocPrinter(hPrinter);
                }
                ClosePrinter(hPrinter);
            }
            // If you did not succeed, GetLastError may give more 
information
            // about why not.
            if (bSuccess == false)
            {
                Marshal.GetLastWin32Error();
            }
            return bSuccess;
        }

        public bool SendFileToPrinter(string szPrinterName, string 
szFileName)
        {
            // Open the file.
            FileStream fs = new FileStream(szFileName, FileMode.Open);
            // Create a BinaryReader on the file.
            BinaryReader br = new BinaryReader(fs);
            // Dim an array of bytes big enough to hold the file's contents.
            Byte[] bytes;
            bool bSuccess;
            // Your unmanaged pointer.
            IntPtr pUnmanagedBytes;
            int nLength;
            documentName = Path.GetFileName(szFileName);
            nLength = Convert.ToInt32(fs.Length);
            // Read the contents of the file into the array.
            bytes = br.ReadBytes(nLength);
            // Allocate some unmanaged memory for those bytes.
            pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
            // Copy the managed byte array into the unmanaged array.
            Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
            // Send the unmanaged bytes to the printer.
            bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, 
nLength);
            // Free the unmanaged memory that you allocated earlier.
            Marshal.FreeCoTaskMem(pUnmanagedBytes);
            fs.Close();
            return bSuccess;
        }

        public bool SendStringToPrinter(string szPrinterName, string 
szString)
        {
            IntPtr pBytes;
            Int32 dwCount;
            // How many characters are in the string?
            dwCount = szString.Length;
            // Assume that the printer is expecting ANSI text, and then 
convert
            // the string to ANSI text.
            pBytes = Marshal.StringToCoTaskMemAnsi(szString);
            // Send the converted ANSI string to the printer.
            SendBytesToPrinter(szPrinterName, pBytes, dwCount);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }

        #region Nested type: DOCINFOA

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public class DOCINFOA
        {
            [MarshalAs(UnmanagedType.LPStr)]
            public string pDataType;

            [MarshalAs(UnmanagedType.LPStr)]
            public string pDocName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pOutputFile;
        }

        #endregion
    }
}

"Ertan Zanagar"  wrote in message 
news:%23V6XaLVAJHA.4512@TK2MSFTNGP04.phx.gbl...
>I have large number of spool files that I need to send to a printer in my 
>.net 2 C# code.
>
> I am trying to write a load testing app that can take these spool files 
> and print them over and over.
>
> What's the best way of doing this print?
>
> Thanks
>
> Ertan Zanagar
>
date: Thu, 21 Aug 2008 11:32:52 -0500   author:   Ertan Zanagar

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us