Example #1
0
 public static PRINTER_INFO_2[] enumPrinters(PrinterEnumFlags 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;
             }
             Marshal.FreeHGlobal(pAddr);
             return printerInfo2;
         }
         lastWin32Error = Marshal.GetLastWin32Error();
     }
     throw new Exception();
 }
Example #2
0
        public static PRINTER_INFO_1[] MyEnumPrinters(PrinterEnumFlags Flags)
        {
            uint cbNeeded = 0;
            uint cReturned = 0;
            IntPtr pPrInfo4 = IntPtr.Zero;
            uint size = 0;

            if (EnumPrinters(Flags, null, 1, IntPtr.Zero, size, ref cbNeeded, ref cReturned))
            {
                return new PRINTER_INFO_1[] { };
            }
            if (cbNeeded != 0)
            {
                pPrInfo4 = Marshal.AllocHGlobal((int)cbNeeded + 128);
                size = cbNeeded + 128;
                EnumPrinters(Flags, null, 1, pPrInfo4, size, ref cbNeeded, ref cReturned);
                if (cReturned != 0)
                {
                    PRINTER_INFO_1[] printerInfo1 = new PRINTER_INFO_1[cReturned];
                    int offset = pPrInfo4.ToInt32();
                    Type type = typeof(PRINTER_INFO_1);
                    int increment = Marshal.SizeOf(type);
                    for (int i = 0; i < cReturned; i++)
                    {
                        printerInfo1[i] = (PRINTER_INFO_1)Marshal.PtrToStructure(new IntPtr(offset), type);
                        offset += increment;
                    }
                    Marshal.FreeHGlobal(pPrInfo4);
                    return printerInfo1;
                }
            }

            return new PRINTER_INFO_1[] { };
        }
Example #3
0
 private static extern bool EnumPrinters(PrinterEnumFlags Flags, string Name, uint Level, IntPtr pPrinterEnum, uint cbBuf, ref uint pcbNeeded, ref uint pcReturned);
        internal static PRINTER_INFO_2[] EnumPrintersByFlag(PrinterEnumFlags Flags)
        {
            uint cbNeeded = 0;
            uint cReturned = 0;
            bool ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);

            IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
            ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);

            if (ret)
            {
                PRINTER_INFO_2[] Info2 = new PRINTER_INFO_2[cReturned];

                int offset = pAddr.ToInt32();

                for (int i = 0; i < cReturned; i++)
                {
                    Info2[i].pServerName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pPrinterName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pShareName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pPortName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pDriverName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pComment = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pLocation = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pDevMode = Marshal.ReadIntPtr(new IntPtr(offset));
                    offset += 4;
                    Info2[i].pSepFile = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pPrintProcessor = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pDatatype = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pParameters = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pSecurityDescriptor = Marshal.ReadIntPtr(new IntPtr(offset));
                    offset += 4;
                    Info2[i].Attributes = (uint)Marshal.ReadIntPtr(new IntPtr(offset));
                    offset += 4;
                    Info2[i].Priority = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].DefaultPriority = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].StartTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].UntilTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].Status = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].cJobs = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].AveragePPM = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;

                }

                Marshal.FreeHGlobal(pAddr);

                return Info2;

            }
            else
            {
                return new PRINTER_INFO_2[0];
            }
        }
Example #5
0
        internal static PRINTER_INFO_2[] EnumPrintersByFlag(PrinterEnumFlags Flags)
        {
            uint cbNeeded  = 0;
            uint cReturned = 0;
            bool ret       = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);

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

            ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);

            if (ret)
            {
                PRINTER_INFO_2[] Info2 = new PRINTER_INFO_2[cReturned];

                int offset = pAddr.ToInt32();

                for (int i = 0; i < cReturned; i++)
                {
                    Info2[i].pServerName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pPrinterName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pShareName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pPortName           = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pDriverName         = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pComment            = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pLocation           = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pDevMode            = Marshal.ReadIntPtr(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].pSepFile            = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pPrintProcessor     = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pDatatype           = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pParameters         = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pSecurityDescriptor = Marshal.ReadIntPtr(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].Attributes          = (uint)Marshal.ReadIntPtr(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].Priority            = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].DefaultPriority     = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].StartTime           = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].UntilTime           = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].Status              = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].cJobs               = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].AveragePPM          = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                }

                Marshal.FreeHGlobal(pAddr);

                return(Info2);
            }
            else
            {
                return(new PRINTER_INFO_2[0]);
            }
        }
Example #6
0
 private static extern bool EnumPrinters(PrinterEnumFlags Flags, string Name, uint Level,
                                         IntPtr pPrinterEnum, uint cbBuf,
                                         ref uint pcbNeeded, ref uint pcReturned);