コード例 #1
0
        protected static void SetDeviceInfo2(string name, PRINTER_INFO_2 newInfo)
        {
            IntPtr          printerHandle;
            PrinterDefaults defaults = new PrinterDefaults {
                DesiredAccess = PRINTER_ACCESS.PrinterAllAccess
            };

            if (!PrintPort.OpenPrinter(name, out printerHandle, ref defaults))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            try
            {
                IntPtr infoHandle = Marshal.AllocHGlobal(Marshal.SizeOf(newInfo));
                Marshal.StructureToPtr(newInfo, infoHandle, false);
                try
                {
                    if (!SetPrinter(printerHandle, 2, infoHandle, 0))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
                catch { throw; }
                finally { Marshal.FreeHGlobal(infoHandle); }
            }
            catch { throw; }
            finally { PrintPort.ClosePrinter(printerHandle); }
        }
コード例 #2
0
        protected static PRINTER_INFO_2[] enumDeviceInfos2(PRINTER_ENUM_FLAGS flags)
        {
            uint cbNeeded  = 0;
            uint cReturned = 0;

            if (EnumPrinters(flags, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned))
            {
                return(null);
            }
            int lastWin32Error = Marshal.GetLastWin32Error();

            if (lastWin32Error == ERROR_INSUFFICIENT_BUFFER)
            {
                IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
                if (EnumPrinters(flags, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned))
                {
                    PRINTER_INFO_2[] printerInfo2 = new PRINTER_INFO_2[cReturned];
                    int  offset    = pAddr.ToInt32();
                    Type type      = typeof(PRINTER_INFO_2);
                    int  increment = Marshal.SizeOf(type);
                    for (int i = 0; i < cReturned; i++)
                    {
                        printerInfo2[i] = (PRINTER_INFO_2)Marshal.PtrToStructure(new IntPtr(offset), type);
                        offset         += increment;
                    }
                    return(printerInfo2);
                }
                Marshal.FreeHGlobal(pAddr);
                lastWin32Error = Marshal.GetLastWin32Error();
            }
            throw new Win32Exception(lastWin32Error);
        }
コード例 #3
0
        protected PRINTER_INFO_2 ToInfo2()
        {
            PRINTER_INFO_2 info = new PRINTER_INFO_2();

            info.pServerName     = ServerName;
            info.pPrinterName    = Name;
            info.pShareName      = ShareName;
            info.pPortName       = PortName;
            info.pDriverName     = DriverName;
            info.pComment        = Description;
            info.pLocation       = Location;
            info.pSepFile        = SeparatorFile;
            info.pPrintProcessor = Processor.Name;
            info.pDatatype       = Processor.DataType;
            info.pParameters     = Processor.Parameters;
            info.Attributes      = Attributes;
            info.Priority        = Priority;
            info.DefaultPriority = DefaultPriority;
            info.StartTime       = StartTime;
            info.UntilTime       = UntilTime;
            info.Status          = Status;
            info.cJobs           = Jobs;
            info.AveragePPM      = AveragePPM;
            return(info);
        }
コード例 #4
0
 protected static void RenameDevice2(string oldName, string newName)
 {
     try
     {
         PRINTER_INFO_2 pi = GetDeviceInfo2(oldName);
         pi.pPrinterName = newName;
         PrintDevice.SetDeviceInfo2(oldName, pi);
     }
     catch { throw; }
 }
コード例 #5
0
 public static void Install(PRINTER_INFO_2 info)
 {
     try
     {
         if (!AddPrinter(null, 2, ref info))
         {
             throw new Win32Exception(Marshal.GetLastWin32Error());
         }
         Spool.Restart();
     }
     catch { throw; }
 }
コード例 #6
0
        public static SmartPrintDevice Install(string name, string description, string portName)
        {
            PRINTER_INFO_2 info = new PRINTER_INFO_2()
            {
                pPrinterName = name,
                pComment     = description,
                pPortName    = portName,
                pDriverName  = DEFAULT_DRIVER_NAME
            };
            SmartPrintDevice device = new SmartPrintDevice(name, description, portName);

            Install(info);
            return(device);
        }
コード例 #7
0
 protected override void FromInfo2(PRINTER_INFO_2 info)
 {
     ServerName    = info.pServerName;
     ShareName     = info.pShareName;
     Description   = info.pComment;
     Location      = info.pLocation;
     SeparatorFile = info.pSepFile;
     Processor     = new PrintProcessor(
         info.pPrintProcessor,
         info.pDatatype,
         info.pParameters);
     Attributes      = info.Attributes;
     Priority        = info.Priority;
     DefaultPriority = info.DefaultPriority;
     StartTime       = info.StartTime;
     UntilTime       = info.UntilTime;
     Status          = info.Status;
     Jobs            = info.cJobs;
     AveragePPM      = info.AveragePPM;
 }
コード例 #8
0
 internal static extern bool AddPrinter(string pName, uint Level, [In] ref PRINTER_INFO_2 pPrinter);