Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

EnumPrinters (winspool)
 
.
Summary

C# Signature:

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

VB Signature:

Declare Function EnumPrinters Lib "winspool.dll" (TODO) As TODO

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

        unsafe public static PRINTER_INFO_2 [] EnumPrinters(PrinterEnumFlags Flags)
        {
            PRINTER_INFO_2 [] Info2;

            uint cbNeeded = 0;
            uint cReturned = 0;
            bool ret = Winspool.EnumPrinters(Flags,
                null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);

            byte [] bPrinterEnum = new byte [cbNeeded];
            fixed (byte *pAddr = bPrinterEnum)
            {

                ret = Winspool.EnumPrinters(Flags,
                    null, 2, (IntPtr)pAddr, (uint)bPrinterEnum.Length, ref cbNeeded, ref cReturned);

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

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

            }
            return Info2;
        }

Alternative Managed API:

PrinterSettings.InstalledPrinters, but this only gives the name of the printers.

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions