Click here to Skip to main content
15,886,689 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.9K   3K   36  
Using SafeHandles to monitor a printer.
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace qPrintComponent
{
    //typedef struct _PRINTER_INFO_5 { 
    //  LPTSTR    pPrinterName;                 :0
    //  LPTSTR    pPortName;                    :1
    //  DWORD     Attributes;                   :2
    //  DWORD     DeviceNotSelectedTimeout;     :3
    //  DWORD     TransmissionRetryTimeout;     :4
    //} PRINTER_INFO_5, *PPRINTER_INFO_5;    

    public sealed class PrinterInfo5 : qSafePrinterInfo
    {
        internal PrinterInfo5(SafeHandle PrinterHandle)
            : base(PrinterHandle,5)
        { }

        public override string ToString()
        {
            return PortName;
        }
        [Description("The name of the printer (local or remote).")]
        [NotifyParentProperty(true)]
        public string PrinterName
        {
            get { return GetStringField(0);}
            set 
            {
                if (value != this.PrinterName)
                {
                    bool b = false;
                    foreach (string name in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
                    {
                        if (name == value)
                            b = true;
                    }
                    if (!b) SetStringField(0, value);
                }
            }
        }
        [Description(@"The port(s) used to transmit data to the printer. If a printer is connected to more than one port, the names of each port must be separated by commas (for example, 'LPT1:,LPT2:,LPT3:').")]
        public string PortName
        {
            get { return GetStringField(1);}
        }
        [Description("The printer attributes.")]
        public qAttributes Attributes
        {
            get { return new qAttributes(this, GetIntField(2));}
        }
        [Description("The maximum time, in milliseconds, allowed to elapse between attempts to select a device.")]
        public int DeviceNotSelectedTimeout
        {
            get { return GetIntField(3); }
        }
        [Description("The maximum time, in milliseconds, allowed to elapse between transmission retries.")]
        public int TransmissionRetryTimeout
        {
            get { return GetIntField(4); }
        }
    }
}

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