Click here to Skip to main content
15,896,453 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 105.1K   3K   36  
Using SafeHandles to monitor a printer.
using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Drawing.Printing;
using System.Security.Permissions;
using System.Runtime.InteropServices;

namespace qPrintComponent
{
    public static class qStatic
    {
        private static TimeSpan span1 = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
        internal static int hourdiv = span1.Hours * 60;
        
        [SecurityCritical, SecurityTreatAsSafe, PrintingPermission(SecurityAction.Demand, Level = PrintingPermissionLevel.DefaultPrinting)]
        public static string GetDefaultPrinter()
        {
            string defaultprinter;
            int num1;
            GetDefaultPrinterW(null, out num1);
            defaultprinter = new string(' ', num1);
            if (GetDefaultPrinterW(defaultprinter, out num1))
                defaultprinter = defaultprinter.TrimEnd('\0');
            return defaultprinter;
        }
        
        [DllImport("winspool.drv", EntryPoint = "OpenPrinterW", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool OpenPrinter(string PrinterName, out IntPtr PrinterHandle, ref qPrinterDefaults PrinterDefaults);

        [DllImport("winspool.drv", EntryPoint = "OpenPrinterW", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool OpenPrinter(string PrinterName, out IntPtr PrinterHandle, int PrinterDefaults);
        
        [DllImport("winspool.Drv", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool ClosePrinter(IntPtr hPrinter);

        [DllImport("winspool.drv", EntryPoint = "GetDefaultPrinterW", CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool GetDefaultPrinterW(string PrinterName, out int dwNeeded);

        [DllImport("winspool.drv", EntryPoint = "SetDefaultPrinter", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool SetDefaultprinter(string Printername);

        [DllImport("winspool.drv", EntryPoint = "GetPrinterW", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool GetPrinter(IntPtr hPrinter, int Level, IntPtr pPrinter, int cbBuf, out int dwNeeded);

        [DllImport("winspool.drv", EntryPoint = "SetPrinterW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool SetPrinter(IntPtr hPrinter, int Level, IntPtr pPrinter, [In, MarshalAs(UnmanagedType.U4)] PrinterControl Command);

        [DllImport("winspool.drv", EntryPoint = "GetPrinterDriverW", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool GetPrinterDriver(IntPtr PrinterHandle, IntPtr pEnvironment, int Level, IntPtr pPrinter, int cbBuf, out int dwNeeded);

        [DllImport("winspool.drv", EntryPoint = "EnumPrinterDriversW", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool EnumPrinterDrivers([In] string ServerName, [In] string Environmentname, [In] int Level, [Out] IntPtr pdrivers, [In] int cbBuf, out int pcbNeeded, out int pcbReturned);

        [DllImport("winspool.drv", EntryPoint = "DeviceCapabilitiesW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        internal static extern int DeviceCapabilitiesW(string sDevice, string Port, int fwCapability, IntPtr Output, IntPtr device);

        [DllImport("winspool.drv", EntryPoint = "FindFirstPrinterChangeNotification", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true)]
        internal static extern IntPtr FindFirstPrinterChangeNotification(qSafePrinterHandle hPrinter, uint fdwFlags, System.UInt32 fdwOptions, [MarshalAs(UnmanagedType.LPStruct)] qPrinterNotifyOptions pPrinterNotifyOptions);

        [DllImport("winspool.drv", EntryPoint = "FindNextPrinterChangeNotification", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        internal static extern Int32 FindNextPrinterChangeNotification(qSafeWaitPrinterHandle hChange, out uint pdwChange, IntPtr PrinterNotifyOptions, ref IntPtr pPrinterNotifyInfo);

        [DllImport("winspool.drv", EntryPoint = "FindNextPrinterChangeNotification", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        internal static extern Int32 FindNextPrinterChangeNotification(qSafeWaitPrinterHandle hChange, out uint pdwChange, qPrinterNotifyOptions PrinterNotifyOptions, ref IntPtr pPrinterNotifyInfo);

        [DllImport("winspool.drv", EntryPoint = "FindClosePrinterChangeNotification", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        internal static extern bool FindClosePrinterChangeNotification(System.IntPtr hChange);

        [DllImport("winspool.drv", EntryPoint = "FreePrinterNotifyInfo", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        internal static extern System.Int32 FreePrinterNotifyInfo(IntPtr pPrinterNotifyInfo);

    }
}

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