Getting Printer Status/Comment like PrintDialog 
Author Message
 Getting Printer Status/Comment like PrintDialog

I am working on a custom form that works like PrintDialog.

What I need is the Status, Type, Where, and Comment information.  Visual
inheritance would be ideal, but the class is sealed.

I have tried various ways to get PRINTER_INFO_2 but all are rather messy.

Any help would be appreciated,

Tue, 08 Mar 2005 03:12:58 GMT  
 Getting Printer Status/Comment like PrintDialog
sometimes we cannot get away from pinvoke.  here is the code for exercising.

using System;
using System.Runtime.InteropServices;

public class Win32
    [DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern bool OpenPrinter(string printer, out IntPtr
handle, IntPtr printerDefaults);

    public static extern bool ClosePrinter(IntPtr handle);

    [DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern bool GetPrinter(IntPtr handle, UInt32 level,
IntPtr buffer,
        UInt32 size, out UInt32 sizeNeeded);

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    public struct PRINTER_INFO_2
        public string pServerName;
        public string pPrinterName;
        public string pShareName;
        public string pPortName;
        public string pDriverName;
        public string pComment;
        public string pLocation;
        public IntPtr pDevMode;
        public string pSepFile;
        public string pPrintProcessor;
        public string pDatatype;
        public string pParameters;
        public IntPtr pSecurityDescriptor;
        public UInt32 Attributes;
        public UInt32 Priority;
        public UInt32 DefaultPriority;
        public UInt32 StartTime;
        public UInt32 UntilTime;
        public UInt32 Status;
        public UInt32 cJobs;
        public UInt32 AveragePPM;


public class printdlg
    public static void Main(string[] args)
        string printer = "\\\\server\\printer";
        IntPtr handle = IntPtr.Zero;
        if(!Win32.OpenPrinter(printer, out handle, IntPtr.Zero))
            System.Console.WriteLine("Fail OpenPrinter: {0}",
        UInt32 level = 2;
        UInt32 sizeNeeded = 0;
        IntPtr buffer = IntPtr.Zero;
        Win32.GetPrinter(handle, level, buffer, 0, out sizeNeeded);
        buffer = Marshal.AllocHGlobal((int)sizeNeeded);
        if(!Win32.GetPrinter(handle, level, buffer, sizeNeeded, out
            System.Console.WriteLine("Fail GetPrinter: {0}",
        Win32.PRINTER_INFO_2 info = (Win32.PRINTER_INFO_2)
            Marshal.PtrToStructure(buffer, typeof(Win32.PRINTER_INFO_2));
        System.Console.WriteLine("status:\t{0}", info.Status);
        System.Console.WriteLine("type:\t{0}", info.pDriverName);
        System.Console.WriteLine("where:\t{0}", info.pLocation);
        System.Console.WriteLine("comment:\t{0}", info.pComment);



This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net?

Fri, 11 Mar 2005 13:25:51 GMT  
 Getting Printer Status/Comment like PrintDialog

> sometimes we cannot get away from pinvoke.  here is the code for

The example works and is a great help.


Fri, 11 Mar 2005 22:02:10 GMT  
 [ 3 post ] 

 Relevant Pages 

1. Getting the status from an HP Printer

2. Getting the printer STATUS through the C program?

3. Getting the Printer Status in NT 4.0

4. getting printer status

5. Getting printer handle from Printer.hDC

6. saving status of collapsible comments

7. block comment fix for gnu indent (pretty printer)...

8. How to get the status of a printer

9. How to get printer status

10. How to Get Printer/Job Status by Win API

11. How to get Printer Status?

12. How to Get Printer/Job Status by Win API


Powered by phpBB® Forum Software