Exemplo n.º 1
0
        private void InstallPrinter(String printerName, String printerPortName, String printerDriverName)
        {
            PrinterMonitorInstallerPInvoke.PRINTER_INFO_2 pInfo = new PrinterMonitorInstallerPInvoke.PRINTER_INFO_2
            {
                pPrinterName    = printerName,
                pPortName       = printerPortName,
                pDriverName     = printerDriverName,
                pPrintProcessor = "WinPrint",
                pShareName      = printerName,
                pDatatype       = "RAW",
                pComment        = printerName,
                Priority        = 1,
                DefaultPriority = 1
            };


            IntPtr hPrt = PrinterMonitorInstallerPInvoke.AddPrinter("", 2, ref pInfo);

            if (hPrt == IntPtr.Zero)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }

            if (PrinterMonitorInstallerPInvoke.ClosePrinter(hPrt) == false)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }
        }
Exemplo n.º 2
0
 private void UninstallPrinterDriver(String printerDriverName)
 {
     if (PrinterMonitorInstallerPInvoke.DeletePrinterDriver(null, null, printerDriverName) == false)
     {
         int errno = Marshal.GetLastWin32Error();
         throw new Win32Exception(errno);
     }
 }
Exemplo n.º 3
0
        private List <String> GetInstalledPrinterDrivers()
        {
            /*
             *  'To determine the required buffer size,
             *  'call EnumPrinterDrivers with cbBuffer set
             *  'to zero. The call will fails specifying
             *  'ERROR_INSUFFICIENT_BUFFER and filling in
             *  'cbRequired with the required size, in bytes,
             *  'of the buffer required to hold the array
             *  'of structures and data.
             */
            uint cbNeeded  = 0;
            uint cReturned = 0;

            if (PrinterMonitorInstallerPInvoke.EnumPrinterDrivers(null, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned))
            {
                //succeeds, but shouldn't, because buffer is zero (too small)!
                throw new Exception("EnumPrinters should fail!");
            }

            int lastWin32Error = Marshal.GetLastWin32Error();

            //ERROR_INSUFFICIENT_BUFFER = 122 expected, if not -> Exception
            if (lastWin32Error != 122)
            {
                throw new Win32Exception(lastWin32Error);
            }

            IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);

            if (PrinterMonitorInstallerPInvoke.EnumPrinterDrivers(null, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned))
            {
                PrinterMonitorInstallerPInvoke.DRIVER_INFO_2[] printerInfo2 = new PrinterMonitorInstallerPInvoke.DRIVER_INFO_2[cReturned];
                int  offset    = pAddr.ToInt32();
                Type type      = typeof(PrinterMonitorInstallerPInvoke.DRIVER_INFO_2);
                int  increment = Marshal.SizeOf(type);
                for (int i = 0; i < cReturned; i++)
                {
                    printerInfo2[i] = (PrinterMonitorInstallerPInvoke.DRIVER_INFO_2)Marshal.PtrToStructure(new IntPtr(offset), type);
                    offset         += increment;
                }
                Marshal.FreeHGlobal(pAddr);

                List <String> result = new List <string>();
                for (int i = 0; i < cReturned; i++)
                {
                    result.Add(printerInfo2[i].pName);
                }
                return(result);
            }

            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
Exemplo n.º 4
0
        private void InstallPrinterDriver(String printerDriverName, String appPath)
        {
            //Parameter siehe http://www.winfaq.de/faq_html/Content/tip2000/onlinefaq.php?h=tip2028.htm
            string parameter = @" /ia /m " + printerDriverName + @" /f """ + appPath + @"gs9.02\lib\printputpdf.inf""";


            int res = PrinterMonitorInstallerPInvoke.PrintUIEntryW(IntPtr.Zero, IntPtr.Zero, parameter, 0);

            if (res != 0)
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                throw new Win32Exception(lastWin32Error);
            }
        }
Exemplo n.º 5
0
        //public List<String> GetInstalledPrintersManaged()
        //{
        //    return PrinterSettings.InstalledPrinters.Cast<string>().ToList();
        //}

        private List <String> GetInstalledPrintersUnmanaged()
        {
            //siehe http://msdn.microsoft.com/en-us/library/dd162692(v=vs.85).aspx

            uint cbNeeded  = 0;
            uint cReturned = 0;

            if (PrinterMonitorInstallerPInvoke.EnumPrinters(PrinterMonitorInstallerPInvoke.PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned))
            {
                //succeeds, but shouldn't, because buffer is zero (too small)!
                throw new Exception("EnumPrinters should fail!");
            }

            int lastWin32Error = Marshal.GetLastWin32Error();

            //ERROR_INSUFFICIENT_BUFFER = 122 expected, if not -> Exception
            if (lastWin32Error != 122)
            {
                throw new Win32Exception(lastWin32Error);
            }

            IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);

            if (PrinterMonitorInstallerPInvoke.EnumPrinters(PrinterMonitorInstallerPInvoke.PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned))
            {
                PrinterMonitorInstallerPInvoke.PRINTER_INFO_2[] printerInfo2 = new PrinterMonitorInstallerPInvoke.PRINTER_INFO_2[cReturned];
                int  offset    = pAddr.ToInt32();
                Type type      = typeof(PrinterMonitorInstallerPInvoke.PRINTER_INFO_2);
                int  increment = Marshal.SizeOf(type);
                for (int i = 0; i < cReturned; i++)
                {
                    printerInfo2[i] = (PrinterMonitorInstallerPInvoke.PRINTER_INFO_2)Marshal.PtrToStructure(new IntPtr(offset), type);
                    offset         += increment;
                }
                Marshal.FreeHGlobal(pAddr);

                List <String> result = new List <string>();
                for (int i = 0; i < cReturned; i++)
                {
                    result.Add(printerInfo2[i].pPrinterName);
                }
                return(result);
            }

            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Uninstalls a printer monitor
        /// </summary>
        /// <param name="monitorName">The Name of the printer monitor</param>
        private void UninstallMonitor(String monitorName)
        {
            //Wenn Monitor gelöscht wird, dann auch automatisch dazugehöriger Port!

            if (PrinterMonitorInstallerPInvoke.DeleteMonitor(null, null, monitorName) == false)
            {
                int errno = Marshal.GetLastWin32Error();

                //3000 == Monitor ist nicht installiert!
                if (errno != 3000)
                {
                    throw new Win32Exception(errno);
                }
                //else
                MessageBox.Show("Could not remove Monitor. Monitor not installed!");
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets list of installed printer ports
        /// </summary>
        /// <returns>A list of installed printer ports</returns>
        private List <String> GetInstalledPorts()
        {
            uint pcbNeeded  = 0;
            uint pcReturned = 0;

            if (PrinterMonitorInstallerPInvoke.EnumPorts(null, 2, IntPtr.Zero, 0, ref pcbNeeded, ref pcReturned))
            {
                //succeeds, but must not, because buffer is zero (too small)!
                throw new Exception("EnumPorts should fail!");
            }

            int lastWin32Error = Marshal.GetLastWin32Error();

            //ERROR_INSUFFICIENT_BUFFER = 122 expected, if not -> Exception
            if (lastWin32Error != 122)
            {
                throw new Win32Exception(lastWin32Error);
            }

            IntPtr pPorts = Marshal.AllocHGlobal((int)pcbNeeded);

            if (PrinterMonitorInstallerPInvoke.EnumPorts(null, 2, pPorts, pcbNeeded, ref pcbNeeded, ref pcReturned))
            {
                IntPtr currentPort = pPorts;
                PrinterMonitorInstallerPInvoke.PORT_INFO_2[] pinfo = new PrinterMonitorInstallerPInvoke.PORT_INFO_2[pcReturned];



                for (int i = 0; i < pcReturned; i++)
                {
                    pinfo[i]    = (PrinterMonitorInstallerPInvoke.PORT_INFO_2)Marshal.PtrToStructure(currentPort, typeof(PrinterMonitorInstallerPInvoke.PORT_INFO_2));
                    currentPort = (IntPtr)(currentPort.ToInt32() + Marshal.SizeOf(typeof(PrinterMonitorInstallerPInvoke.PORT_INFO_2)));
                }
                Marshal.FreeHGlobal(pPorts);

                List <String> result = new List <string>();
                for (int i = 0; i < pcReturned; i++)
                {
                    result.Add(pinfo[i].pPortName);
                }
                return(result);
            }

            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
Exemplo n.º 8
0
        /// <summary>
        /// Installs a printer monitor
        /// </summary>
        /// <param name="monitorName">The Name of the printer monitor</param>
        /// <param name="pathToMonitorDLL">The Path to the Monitor-DLL</param>
        private void InstallMonitor(String monitorName, String pathToMonitorDLL)
        {
            PrinterMonitorInstallerPInvoke.MONITOR_INFO_2 mi2;
            mi2.pName        = monitorName;
            mi2.pEnvironment = null;
            mi2.pDLLName     = pathToMonitorDLL;

            if (PrinterMonitorInstallerPInvoke.AddMonitor(null, 2, ref mi2) == 0)
            {
                int errno = Marshal.GetLastWin32Error();

                //3006 == Monitor bereits installiert!
                if (errno != 3006)
                {
                    throw new Win32Exception(errno);
                }
                //else
                MessageBox.Show("Could not install monitor. Monitor already installed!");
            }
        }
Exemplo n.º 9
0
        private void RemoveJobs(String printerName)
        {
            IntPtr hPrinter;

            PrinterMonitorInstallerPInvoke.PRINTER_DEFAULTS defaults = new PrinterMonitorInstallerPInvoke.PRINTER_DEFAULTS
            {
                DesiredAccess = 0x000F000C, //PRINTER_ALL_ACCESS
                pDatatype     = IntPtr.Zero,
                pDevMode      = IntPtr.Zero
            };


            if (PrinterMonitorInstallerPInvoke.OpenPrinter(printerName, out hPrinter, ref defaults) == false)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }

            if (hPrinter == IntPtr.Zero)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }

            //3 == PRINTER_CONTROL_PURGE => Entfernt alle Jobs!
            if (PrinterMonitorInstallerPInvoke.SetPrinter(hPrinter, 0, IntPtr.Zero, PrinterMonitorInstallerPInvoke.PRINTER_CONTROL_PURGE) == false)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }

            if (PrinterMonitorInstallerPInvoke.ClosePrinter(hPrinter) == false)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }
        }
Exemplo n.º 10
0
        private void UninstallPrinter(String printerName)
        {
            IntPtr hPrinter;

            PrinterMonitorInstallerPInvoke.PRINTER_DEFAULTS defaults = new PrinterMonitorInstallerPInvoke.PRINTER_DEFAULTS
            {
                DesiredAccess = 0x000F000C,  //PRINTER_ALL_ACCESS
                pDatatype     = IntPtr.Zero,
                pDevMode      = IntPtr.Zero
            };


            if (PrinterMonitorInstallerPInvoke.OpenPrinter(printerName, out hPrinter, ref defaults) == false)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }

            if (hPrinter == IntPtr.Zero)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }

            if (PrinterMonitorInstallerPInvoke.DeletePrinter(hPrinter) == false)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }

            if (PrinterMonitorInstallerPInvoke.ClosePrinter(hPrinter) == false)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }
        }