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
        //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());
        }