Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Network_Printer
{
    class Program
    {

        static void Main(string[] args)
        {
            // >> Local printers list
            Console.WriteLine("Local printers list:");

            string[] ListLocal;
            ListLocal = clPrintersList.GetPrinterNames(clPrintersList.PrinterEnumFlags.PRINTER_ENUM_LOCAL);
            int count = 1;

            foreach (string name in ListLocal)
            {
                Console.WriteLine("Device {0} local - {1}", count, name);
                count += 1;
            }


            // >> "Connected" printers list
            Console.WriteLine("\nConnected printers list:");

            string[] ListCon;
            ListCon = clPrintersList.GetPrinterNames(clPrintersList.PrinterEnumFlags.PRINTER_ENUM_CONNECTIONS);
            int count2 = 1;

            foreach (string name in ListCon)
            {
                Console.WriteLine("Device {0} connected - {1}", count2, name);
                count2 += 1;
            }
            // >> Remote printers list
            Console.WriteLine("Remote printers list:");

            string[] Listremote;

//<big> Below Line Should Provide Information for remote printer But Is Unable to do so, Yet window can find this printer without mentioning name of Printer </big>
            Listremote = clPrintersList.GetPrinterNames(clPrintersList.PrinterEnumFlags.PRINTER_ENUM_REMOTE);
            int count3 = 1;

            foreach (string name in Listremote)
            {
                Console.WriteLine("Device {0} Remote - {1}", count3, name);
                count3 += 1;
            }
            Console.WriteLine("\nPress any key...");
            Console.ReadKey();

        }

    }
    internal class clPrintersList
    {
        [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool EnumPrintersA(
        PrinterEnumFlags Flags,
        string Name,
        uint Level,
        IntPtr pPrinterEnum,
        uint cbBuf,
        ref uint pcbNeeded,
        ref uint pcReturned);


        public static string[] GetPrinterNames(PrinterEnumFlags PEF)
        {
            if (PEF == PrinterEnumFlags.PRINTER_ENUM_REMOTE)
            {
                List<string> returnVal = new List<string>();
                foreach (PRINTER_INFO_2 info in EnumPrinters(PEF))
                {
                    returnVal.Add(info.pName);
                }
                return returnVal.ToArray();
            }
            else
            {
                List<string> returnVal = new List<string>();
                foreach (PRINTER_INFO_2 info in EnumPrinters1(PEF))
                {
                    returnVal.Add(info.pName);
                }
                return returnVal.ToArray();
            }
        }
        private static PRINTER_INFO_2[] EnumPrinters(PrinterEnumFlags Flags)
        {
            uint cbNeeded = 0;
            uint cReturned = 0;
            if (EnumPrintersA(Flags, "", 1, IntPtr.Zero, 0, ref cbNeeded, ref cReturned))
            {
                return null;
            }
            int lastWin32Error = Marshal.GetLastWin32Error();
            if (lastWin32Error != 123)
            {
                IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
                if (EnumPrintersA(Flags, "", 1, pAddr, cbNeeded, ref cbNeeded, ref cReturned))
                {
                    PRINTER_INFO_2[] printerInfo1 = 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++)
                    {
                        printerInfo1[i] = (PRINTER_INFO_2)Marshal.PtrToStructure(new IntPtr(offset), type);
                        offset += increment;
                    }
                    Marshal.FreeHGlobal(pAddr);
                    return printerInfo1;
                }
                lastWin32Error = Marshal.GetLastWin32Error();
            }
            throw new System.ComponentModel.Win32Exception(lastWin32Error);
        }
        private static PRINTER_INFO_2[] EnumPrinters1(PrinterEnumFlags Flags)
        {
            uint cbNeeded = 0;
            uint cReturned = 0;
            if (EnumPrintersA(Flags, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned))
            {
                return null;
            }
            int lastWin32Error = Marshal.GetLastWin32Error();
            if (lastWin32Error != 123)
            {
                IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
                if (EnumPrintersA(Flags, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned))
                {
                    PRINTER_INFO_2[] printerInfo1 = 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++)
                    {
                        printerInfo1[i] = (PRINTER_INFO_2)Marshal.PtrToStructure(new IntPtr(offset), type);
                        offset += increment;
                    }
                    Marshal.FreeHGlobal(pAddr);
                    return printerInfo1;
                }
                lastWin32Error = Marshal.GetLastWin32Error();
            }
            throw new System.ComponentModel.Win32Exception(lastWin32Error);
            
        }

        private struct PRINTER_INFO_2
        {
            [MarshalAs(UnmanagedType.LPStr)]
            public string pServerName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pShareName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pPortName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pDriverName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pComment;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pLocation;
            public IntPtr pDevMode;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pSepFile;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pPrintProcessor;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pDatatype;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pParameters;
            public IntPtr pSecurityDescriptor;
            public Int32 Attributes;
            public Int32 Priority;
            public Int32 DefaultPriority;
            public Int32 StartTime;
            public Int32 UntilTime;
            public Int32 Status;
            public Int32 cJobs;
            public Int32 AveragePPM;
        }


        [FlagsAttribute]
        internal enum PrinterEnumFlags
        {
            PRINTER_ENUM_DEFAULT = 0x00000001,
            PRINTER_ENUM_LOCAL = 0x00000002,
            PRINTER_ENUM_CONNECTIONS = 0x00000004,
            PRINTER_ENUM_FAVORITE = 0x00000004,
            PRINTER_ENUM_NAME = 0x00000008,
            PRINTER_ENUM_REMOTE = 0x00000010,
            PRINTER_ENUM_SHARED = 0x00000020,
            PRINTER_ENUM_NETWORK = 0x00000040,
            PRINTER_ENUM_EXPAND = 0x00004000,
            PRINTER_ENUM_CONTAINER = 0x00008000,
            PRINTER_ENUM_ICONMASK = 0x00ff0000,
            PRINTER_ENUM_ICON1 = 0x00010000,
            PRINTER_ENUM_ICON2 = 0x00020000,
            PRINTER_ENUM_ICON3 = 0x00040000,
            PRINTER_ENUM_ICON4 = 0x00080000,
            PRINTER_ENUM_ICON5 = 0x00100000,
            PRINTER_ENUM_ICON6 = 0x00200000,
            PRINTER_ENUM_ICON7 = 0x00400000,
            PRINTER_ENUM_ICON8 = 0x00800000,
            PRINTER_ENUM_HIDE = 0x01000000
        }


    }
}
Posted
Updated 23-Jul-13 0:23am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900