Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

Printers and SafeHandles

Rate me:
Please Sign up or sign in to vote.
4.84/5 (16 votes)
16 Apr 20076 min read 130.3K   3.4K   78  
Using SafeHandles to access PrinterInfo and DriverInfo structures.
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Security;
using System.Drawing.Printing;
using System.ComponentModel;

namespace qPrintComponent
{
    public abstract class qSafePrinterInfo : qSafeInfo
    {
        
        protected qSafePrinterInfo(SafeHandle PrinterHandle, int level)
            :base(PrinterHandle, level)
        {            
        }

        [PrintingPermission(SecurityAction.Demand, Level = PrintingPermissionLevel.DefaultPrinting)]
        protected override void RefreshInfo()
        {
            if (!qStatic.GetPrinter(PrinterHandle, Level, handle, Size, out BytesNeeded) && (BytesNeeded > 0))
            {
                AllocMem();
                if (!qStatic.GetPrinter(PrinterHandle, Level, handle, Size, out BytesNeeded) && (BytesNeeded > 0))
                    throw new Win32Exception(GetType().FullName + Level.ToString(System.Globalization.CultureInfo.InvariantCulture) + " Error");
            }
        }

        [PrintingPermission(SecurityAction.Demand, Level = PrintingPermissionLevel.AllPrinting)]
        private bool UpdatePrinter(int printerLevel, IntPtr memhandle, PrinterControl pc)
        {
            if (!PrintComponent.SafePrinterHandle.canUpdate)
            {
                System.Windows.Forms.MessageBox.Show("Can not Update");
                return false;
            }
            if (this.IsInvalid)
                throw new InvalidOperationException("Invalid memoryhandle");
            if (PrinterSafeHandle.IsInvalid)
                throw new InvalidOperationException("Printer SafeHandle Invalid");
            bool b = qStatic.SetPrinter(PrinterHandle, printerLevel, memhandle, pc);
            if (!b)
                throw new Win32Exception("Printer NOT Updated. Reason:" + Marshal.GetLastWin32Error().ToString(System.Globalization.CultureInfo.InvariantCulture));
            this.RefreshInfo();
            return b;
        }

        public bool PausePrinter()
        {
            return UpdatePrinter(0, IntPtr.Zero, PrinterControl.Pause);
        }
        public bool ResumePrinter()
        {
            return UpdatePrinter(0, IntPtr.Zero, PrinterControl.Resume);
        }
        public bool PurgePrinter()
        {
            return UpdatePrinter(0, IntPtr.Zero, PrinterControl.Purge);
        }
        protected override bool Update()
        {
            return UpdatePrinter(Level, this.handle, PrinterControl.Nul);
        }
        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions