예제 #1
0
        /// <summary>
        /// Creates a new printer on the system
        /// </summary>
        /// <param name="printerName">The name of the printer</param>
        /// <param name="portName">The port to listen on</param>
        /// <param name="driverName">The name of the print driver to use</param>
        /// <param name="printProcessor">The processor to use</param>
        public void CreatePrinter(string printerName, string portName, string driverName, string printProcessor)
        {
            Precondition.NotNullOrEmpty(printerName, "printerName");
            Precondition.NotNullOrEmpty(portName, "portName");
            Precondition.NotNullOrEmpty(driverName, "driverName");
            Precondition.NotNullOrEmpty(printProcessor, "printProcessor");

            SafeNativeMethods.PRINTER_INFO_2 _pInfo = new SafeNativeMethods.PRINTER_INFO_2
            {
                pPrinterName    = printerName,
                pPortName       = portName,
                pDriverName     = driverName,
                pPrintProcessor =
                    printProcessor
            };

            IntPtr hPrinter = SafeNativeMethods.AddPrinter(null, 2, ref _pInfo);

            if (hPrinter != IntPtr.Zero)
            {
                SafeNativeMethods.ClosePrinter(hPrinter);

                Logger.InfoFormat("Printer created with name {0} using port: {1} and driver {2}", printerName, portName, driverName);
            }
            else
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }