Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

Printers and SafeHandles (Part 2)

Rate me:
Please Sign up or sign in to vote.
3.00/5 (7 votes)
16 Apr 20073 min read 104.8K   3K   36  
Using SafeHandles to monitor a printer.
using System;
using System.ComponentModel;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
using System.Security.Permissions;

namespace qPrintComponent
{
    public sealed class qSafePrinterHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        
        private DriverInfo1 _DriverInfo1 ;
        private DriverInfo2 _DriverInfo2;
        private DriverInfo3 _DriverInfo3;
        private DriverInfo4 _DriverInfo4;
        private DriverInfo5 _DriverInfo5;
        private DriverInfo6 _DriverInfo6;
        private PrinterInfo1 _PrinterInfo1;
        private PrinterInfo2 _PrinterInfo2;
        private PrinterInfo4 _PrinterInfo4;
        private PrinterInfo5 _PrinterInfo5;
        private PrinterInfo6 _PrinterInfo6;

        private PrinterInfoQ _PrinterInfoQ;
        
        private qPrinterDefaults _PrinterDefaults = new qPrinterDefaults(true);

        internal qSafePrinterHandle() 
            : base(true) 
        {}

        internal qSafePrinterHandle(string printername)
            : base(true)
        {
            qStatic.OpenPrinter(printername, out this.handle, ref _PrinterDefaults);
            ResetPrinterInfo(); 
        }

        [return: MarshalAs(UnmanagedType.U1)]
        protected override bool ReleaseHandle()
        {
            // Only close printer if handle is valid
            if (IsInvalid) 
                return true;
            // If the closing failes, the ReleaseHandleFailed MDA will be activated
            if (!qStatic.ClosePrinter(this.handle))
                return false;
            this.SetHandle(IntPtr.Zero);
            return true; 
        }
     

        [TypeConverter(typeof(PrinterNameConverter))]
        public string PrinterName
        {
            get
            {
                if (this.IsInvalid) return "";
                return PrinterInfo5.PrinterName;
            }
            set
            {
                if (value != PrinterName)
                {
                    this.ReleaseHandle();
                    qStatic.OpenPrinter(value, out this.handle, ref _PrinterDefaults);
                    ResetPrinterInfo();
                }
            }
        }
        public void ResetPrinterInfo()
        {
            if (!IsInvalid)
                _PrinterInfo5 = new PrinterInfo5(this);
            _DriverInfo1 = null;
            _DriverInfo2 = null;
            _DriverInfo3 = null;
            _DriverInfo4 = null;
            _DriverInfo5 = null;
            _DriverInfo6 = null;
            _PrinterInfo1 = null;
            _PrinterInfo2 = null;
            _PrinterInfo4 = null;
            _PrinterInfo6 = null;
            _PrinterInfoQ = null;
        }

        [Browsable(false)]
        internal string PortName
        {
            get 
            {
                if (this.IsInvalid) return "";
                return PrinterInfo5.PortName;
            }
        }
        [Category("Printer"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        public PrinterInfo1 PrinterInfo1
        {
            get
            {
                if (this.IsInvalid) return null;
                if (_PrinterInfo1 == null) _PrinterInfo1 = new PrinterInfo1(this); 
                return _PrinterInfo1;
            }
        }
        [Category("Printer"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        [Description("Maximal printer information")]
        public PrinterInfo2 PrinterInfo2
        {
            get
            {
                if (this.IsInvalid) return null;
                if (_PrinterInfo2 == null) _PrinterInfo2 = new PrinterInfo2(this);
                return _PrinterInfo2;
            }
        }
        [Category("Printer"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        [Description("Minimal printer information")]
        public PrinterInfo4 PrinterInfo4
        {
            get
            {
                if (this.IsInvalid) return null;
                if (_PrinterInfo4 == null) _PrinterInfo4 = new PrinterInfo4(this);
                return _PrinterInfo4;
            }
        }
        [Category("Printer"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        public PrinterInfo5 PrinterInfo5
        {
            get
            {
                if (this.IsInvalid) return null;
                if (_PrinterInfo5 == null) _PrinterInfo5 = new PrinterInfo5(this); 
                return _PrinterInfo5;
            }
        }
        [Category("Printer"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        [Description("The status value of a printer.")]
        public PrinterInfo6 PrinterInfo6
        {
            get
            {
                if (this.IsInvalid) return null; 
                if (_PrinterInfo6 == null) _PrinterInfo6 = new PrinterInfo6(this);
                return _PrinterInfo6;
            }
        }
        [Category("Printer"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        public PrinterInfoQ PrinterInfoQ
        {
            get
            {
                if (this.IsInvalid) return null;
                if (_PrinterInfoQ == null) _PrinterInfoQ = new PrinterInfoQ(_PrinterInfo5);
                return _PrinterInfoQ;
            }
        }
        [Category("Driver"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        public DriverInfo1 DriverInfo1
        {
            get
            {
                if (this.IsInvalid) return null;
                if (_DriverInfo1 == null) _DriverInfo1 = new DriverInfo1(this);
                return _DriverInfo1;
            }
        }
        [Category("Driver"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        public DriverInfo2 DriverInfo2
        {
            get
            {
                if (this.IsInvalid) return null;
                if (_DriverInfo2 == null) _DriverInfo2 = new DriverInfo2(this);
                return _DriverInfo2;
            }
        }
        [Category("Driver"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        public DriverInfo3 DriverInfo3
        {
            get
            {
                if (this.IsInvalid) return null;
                if (_DriverInfo3 == null) _DriverInfo3 = new DriverInfo3(this); 
                return _DriverInfo3;
            }
        }
        [Category("Driver"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        public DriverInfo4 DriverInfo4
        {
            get
            {
                if (this.IsInvalid) return null;
                if (_DriverInfo4 == null) _DriverInfo4 = new DriverInfo4(this); 
                return _DriverInfo4;
            }
        }
        [Category("Driver"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        public DriverInfo5 DriverInfo5
        {
            get
            {
                if (this.IsInvalid) return null;
                if (_DriverInfo5 == null) _DriverInfo5 = new DriverInfo5(this); 
                return _DriverInfo5;
            }
        }
        [Category("Driver"), TypeConverter(typeof(ExpandableObjectConverter)), Browsable(true)]
        public DriverInfo6 DriverInfo6
        {
            get
            {
                if (this.IsInvalid) return null;
                if (_DriverInfo6 == null) _DriverInfo6 = new DriverInfo6(this);
                return _DriverInfo6;
            }
        }
   }  
}

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