Printer Status

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gordon Truslove

    #1

    Printer Status

    Has anybody managed to get GetPrinter and PRINER_INFO_2 to work?
    I would like to retrieve the printer status.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Printer Status

    Gordon,

    What are the definitions you are using? The PRINTER_INFO_2 structure
    has a lot of information (if you include the DEVMODE structure as well), and
    there could be errors in how you are accessing it.

    Can you show the definitions that you have?


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "Gordon Truslove" <Gordon.Truslov e@QCL-Solutions.com> wrote in message
    news:OSTLwNxjDH A.2424@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Has anybody managed to get GetPrinter and PRINER_INFO_2 to work?
    > I would like to retrieve the printer status.
    >
    >[/color]


    Comment

    • Gordon Truslove

      #3
      Re: Printer Status

      I've tried about 3-4 variations. Here is the biggest. I've tried avoiding
      the devmode and security descriptor but it still fails.
      [ DllImport("wins pool.drv" )]

      public static extern bool GetPrinter(IntP tr hPrinter,long Level,ref
      PRINTERINFO2 pPrinter,long cbBuf,ref long pcbNeeded);


      [StructLayout( LayoutKind.Sequ ential)]

      public struct PRINTERINFO2

      {

      [MarshalAs(Unman agedType.LPWStr )]public string pServerName;

      [MarshalAs(Unman agedType.LPWStr )]public string pPrinterName;

      [MarshalAs(Unman agedType.LPWStr )]public string pShareName;

      [MarshalAs(Unman agedType.LPWStr )]public string pPortName;

      [MarshalAs(Unman agedType.LPWStr )]public string pDriverName;

      [MarshalAs(Unman agedType.LPWStr )]public string pComment;

      [MarshalAs(Unman agedType.LPWStr )]public string pLocation;

      public DEVMODE pDevMode;

      [MarshalAs(Unman agedType.LPWStr )]public string pSepFile;

      [MarshalAs(Unman agedType.LPWStr )]public string pPrintProcessor ;

      [MarshalAs(Unman agedType.LPWStr )]public string pDatatype;

      [MarshalAs(Unman agedType.LPWStr )]public string pParameters;

      public SECURITY_DESCRI PTOR pSecurityDescri ptor;

      public long Attributes;

      public long Priority;

      public long DefaultPriority ;

      public long StartTime;

      public long UntilTime;

      public long Status;

      public long cJobs;

      public long AveragePPM;

      }

      [StructLayout( LayoutKind.Sequ ential)]

      public struct SECURITY_DESCRI PTOR{

      public byte Revision;

      public byte Sbz1;

      public long Control;

      public long Owner;

      public long Group;

      public ACL sACL;

      public ACL Dacl;

      }

      [StructLayout( LayoutKind.Sequ ential)]

      public struct ACL{

      public byte AclRevision;

      public byte Sbz1;

      public int AclSize;

      public int AceCount;

      public int Sbz2;

      }



      [StructLayout( LayoutKind.Sequ ential)]

      public struct DEVMODE{

      public string dmDeviceName;

      public int dmSpecVersion;

      public int dmDriverVersion ;

      public int dmSize;

      public int dmDriverExtra;

      public long dmFields;

      public int dmOrientation;

      public int dmPaperSize;

      public int dmPaperLength;

      public int dmPaperWidth;

      public int dmScale;

      public int dmCopies;

      public int dmDefaultSource ;

      public int dmPrintQuality;

      public int dmColor;

      public int dmDuplex;

      public int dmYResolution;

      public int dmTTOption;

      public int dmCollate;

      public string dmFormName;

      public int dmLogPixels;

      public long dmBitsPerPel;

      public long dmPelsWidth;

      public long dmPelsHeight;

      public long dmDisplayFlags;

      public long dmDisplayFreque ncy;

      public long dmICMMethod;

      public long dmICMIntent;

      public long dmMediaType;

      public long dmDitherType;

      public long dmReserved1;

      public long dmReserved2;

      }

      "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
      in message news:%23pI8M2zj DHA.2424@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > Gordon,
      >
      > What are the definitions you are using? The PRINTER_INFO_2 structure
      > has a lot of information (if you include the DEVMODE structure as well),[/color]
      and[color=blue]
      > there could be errors in how you are accessing it.
      >
      > Can you show the definitions that you have?
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - nick(dot)paldin o=at=exisconsul ting<dot>com
      >
      > "Gordon Truslove" <Gordon.Truslov e@QCL-Solutions.com> wrote in message
      > news:OSTLwNxjDH A.2424@TK2MSFTN GP10.phx.gbl...[color=green]
      > > Has anybody managed to get GetPrinter and PRINER_INFO_2 to work?
      > > I would like to retrieve the printer status.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Printer Status

        Gordon,

        Ahh...

        The first thing is that all of the places you are using long you should
        be using int. In C, a long is a 32-bit integer. In C#, a long is a 64-bit
        integer, which is throwing everything off. So you should be changing the
        longs to ints.

        Additionally, you should set the unmanaged type to LPTStr and set the
        CharSet value in the StructLayout attribute to CharSet.Auto (as well as in
        the DllImport declaration as well).

        Finally, the SECURITY_DESCRI PTOR and DEVMODE structures are not embedded
        in the PRINTER_INFO_2 structure. Rather, they are pointers. You can
        replace these fields with IntPtr and set them to zero if you are not using
        them.

        However, when the call is complete, you need to be aware if they were
        populated and free the memory that the pointers point to if there is
        something there.

        Hope this helps.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - nick(dot)paldin o=at=exisconsul ting<dot>com


        "Gordon Truslove" <Gordon.Truslov e@QCL-Solutions.com> wrote in message
        news:uzTFV6zjDH A.2964@tk2msftn gp13.phx.gbl...[color=blue]
        > I've tried about 3-4 variations. Here is the biggest. I've tried avoiding
        > the devmode and security descriptor but it still fails.
        > [ DllImport("wins pool.drv" )]
        >
        > public static extern bool GetPrinter(IntP tr hPrinter,long Level,ref
        > PRINTERINFO2 pPrinter,long cbBuf,ref long pcbNeeded);
        >
        >
        > [StructLayout( LayoutKind.Sequ ential)]
        >
        > public struct PRINTERINFO2
        >
        > {
        >
        > [MarshalAs(Unman agedType.LPWStr )]public string pServerName;
        >
        > [MarshalAs(Unman agedType.LPWStr )]public string pPrinterName;
        >
        > [MarshalAs(Unman agedType.LPWStr )]public string pShareName;
        >
        > [MarshalAs(Unman agedType.LPWStr )]public string pPortName;
        >
        > [MarshalAs(Unman agedType.LPWStr )]public string pDriverName;
        >
        > [MarshalAs(Unman agedType.LPWStr )]public string pComment;
        >
        > [MarshalAs(Unman agedType.LPWStr )]public string pLocation;
        >
        > public DEVMODE pDevMode;
        >
        > [MarshalAs(Unman agedType.LPWStr )]public string pSepFile;
        >
        > [MarshalAs(Unman agedType.LPWStr )]public string pPrintProcessor ;
        >
        > [MarshalAs(Unman agedType.LPWStr )]public string pDatatype;
        >
        > [MarshalAs(Unman agedType.LPWStr )]public string pParameters;
        >
        > public SECURITY_DESCRI PTOR pSecurityDescri ptor;
        >
        > public long Attributes;
        >
        > public long Priority;
        >
        > public long DefaultPriority ;
        >
        > public long StartTime;
        >
        > public long UntilTime;
        >
        > public long Status;
        >
        > public long cJobs;
        >
        > public long AveragePPM;
        >
        > }
        >
        > [StructLayout( LayoutKind.Sequ ential)]
        >
        > public struct SECURITY_DESCRI PTOR{
        >
        > public byte Revision;
        >
        > public byte Sbz1;
        >
        > public long Control;
        >
        > public long Owner;
        >
        > public long Group;
        >
        > public ACL sACL;
        >
        > public ACL Dacl;
        >
        > }
        >
        > [StructLayout( LayoutKind.Sequ ential)]
        >
        > public struct ACL{
        >
        > public byte AclRevision;
        >
        > public byte Sbz1;
        >
        > public int AclSize;
        >
        > public int AceCount;
        >
        > public int Sbz2;
        >
        > }
        >
        >
        >
        > [StructLayout( LayoutKind.Sequ ential)]
        >
        > public struct DEVMODE{
        >
        > public string dmDeviceName;
        >
        > public int dmSpecVersion;
        >
        > public int dmDriverVersion ;
        >
        > public int dmSize;
        >
        > public int dmDriverExtra;
        >
        > public long dmFields;
        >
        > public int dmOrientation;
        >
        > public int dmPaperSize;
        >
        > public int dmPaperLength;
        >
        > public int dmPaperWidth;
        >
        > public int dmScale;
        >
        > public int dmCopies;
        >
        > public int dmDefaultSource ;
        >
        > public int dmPrintQuality;
        >
        > public int dmColor;
        >
        > public int dmDuplex;
        >
        > public int dmYResolution;
        >
        > public int dmTTOption;
        >
        > public int dmCollate;
        >
        > public string dmFormName;
        >
        > public int dmLogPixels;
        >
        > public long dmBitsPerPel;
        >
        > public long dmPelsWidth;
        >
        > public long dmPelsHeight;
        >
        > public long dmDisplayFlags;
        >
        > public long dmDisplayFreque ncy;
        >
        > public long dmICMMethod;
        >
        > public long dmICMIntent;
        >
        > public long dmMediaType;
        >
        > public long dmDitherType;
        >
        > public long dmReserved1;
        >
        > public long dmReserved2;
        >
        > }
        >
        > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
        wrote[color=blue]
        > in message news:%23pI8M2zj DHA.2424@TK2MSF TNGP10.phx.gbl. ..[color=green]
        > > Gordon,
        > >
        > > What are the definitions you are using? The PRINTER_INFO_2[/color][/color]
        structure[color=blue][color=green]
        > > has a lot of information (if you include the DEVMODE structure as well),[/color]
        > and[color=green]
        > > there could be errors in how you are accessing it.
        > >
        > > Can you show the definitions that you have?
        > >
        > >
        > > --
        > > - Nicholas Paldino [.NET/C# MVP]
        > > - nick(dot)paldin o=at=exisconsul ting<dot>com
        > >
        > > "Gordon Truslove" <Gordon.Truslov e@QCL-Solutions.com> wrote in message
        > > news:OSTLwNxjDH A.2424@TK2MSFTN GP10.phx.gbl...[color=darkred]
        > > > Has anybody managed to get GetPrinter and PRINER_INFO_2 to work?
        > > > I would like to retrieve the printer status.
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Gordon Truslove

          #5
          Re: Printer Status

          Well I'm getting closer. ifferent error this time

          122 - The data area passed to a system call is too small.

          [StructLayout( LayoutKind.Sequ ential,CharSet= CharSet.Auto)]

          public struct PRINTERINFO2

          {

          [MarshalAs(Unman agedType.LPTStr )]public string pServerName;

          [MarshalAs(Unman agedType.LPTStr )]public string pPrinterName;

          [MarshalAs(Unman agedType.LPTStr )]public string pShareName;

          [MarshalAs(Unman agedType.LPTStr )]public string pPortName;

          [MarshalAs(Unman agedType.LPTStr )]public string pDriverName;

          [MarshalAs(Unman agedType.LPTStr )]public string pComment;

          [MarshalAs(Unman agedType.LPTStr )]public string pLocation;

          public IntPtr pDevMode;

          [MarshalAs(Unman agedType.LPTStr )]public string pSepFile;

          [MarshalAs(Unman agedType.LPTStr )]public string pPrintProcessor ;

          [MarshalAs(Unman agedType.LPTStr )]public string pDatatype;

          [MarshalAs(Unman agedType.LPTStr )]public string pParameters;

          public IntPtr pSecurityDescri ptor;

          public int Attributes;

          public int Priority;

          public int DefaultPriority ;

          public int StartTime;

          public int UntilTime;

          public int Status;

          public int cJobs;

          public int AveragePPM;

          }

          public class Printer

          {

          [ DllImport( "winspool.drv", CharSet=CharSet .Unicode,ExactS pelling=false,

          CallingConventi on=CallingConve ntion.StdCall )]

          public static extern long OpenPrinter(str ing pPrinterName,re f IntPtr
          phPrinter, int pDefault);

          [ DllImport(

          "winspool.drv", CharSet=CharSet .Unicode,ExactS pelling=true,

          CallingConventi on=CallingConve ntion.StdCall )]

          public static extern long ClosePrinter(In tPtr hPrinter);

          [ DllImport(

          "winspool.d rv" ,CharSet=CharSe t.Auto)]

          public static extern bool GetPrinter(IntP tr hPrinter,int Level,ref
          PRINTERINFO2 pPrinter,int cbBuf,ref int pcbNeeded);

          }

          public class App

          {

          public static void Main ()

          {

          System.IntPtr lhPrinter=new System.IntPtr() ;

          Printer.OpenPri nter("Epson EPL-5700",ref lhPrinter,0);

          PRINTERINFO2 PI=new PRINTERINFO2();

          int Need=0;

          Console.WriteLi ne(Printer.GetP rinter(lhPrinte r,2,ref
          PI,Marshal.Size Of(PI),ref Need));


          Console.WriteLi ne("Error "+Marshal.GetLa stWin32Error()) ;


          Printer.ClosePr inter(lhPrinter );

          Console.ReadLin e();

          }

          }


          Comment

          • Gordon Truslove

            #6
            Re: Printer Status

            Well I'm getting closer. different error this time

            122 - The data area passed to a system call is too small.

            [StructLayout( LayoutKind.Sequ ential,CharSet= CharSet.Auto)]

            public struct PRINTERINFO2

            {

            [MarshalAs(Unman agedType.LPTStr )]public string pServerName;

            [MarshalAs(Unman agedType.LPTStr )]public string pPrinterName;

            [MarshalAs(Unman agedType.LPTStr )]public string pShareName;

            [MarshalAs(Unman agedType.LPTStr )]public string pPortName;

            [MarshalAs(Unman agedType.LPTStr )]public string pDriverName;

            [MarshalAs(Unman agedType.LPTStr )]public string pComment;

            [MarshalAs(Unman agedType.LPTStr )]public string pLocation;

            public IntPtr pDevMode;

            [MarshalAs(Unman agedType.LPTStr )]public string pSepFile;

            [MarshalAs(Unman agedType.LPTStr )]public string pPrintProcessor ;

            [MarshalAs(Unman agedType.LPTStr )]public string pDatatype;

            [MarshalAs(Unman agedType.LPTStr )]public string pParameters;

            public IntPtr pSecurityDescri ptor;

            public int Attributes;

            public int Priority;

            public int DefaultPriority ;

            public int StartTime;

            public int UntilTime;

            public int Status;

            public int cJobs;

            public int AveragePPM;

            }

            public class Printer

            {

            [ DllImport( "winspool.drv", CharSet=CharSet .Unicode,ExactS pelling=false,

            CallingConventi on=CallingConve ntion.StdCall )]

            public static extern long OpenPrinter(str ing pPrinterName,re f IntPtr
            phPrinter, int pDefault);

            [ DllImport(

            "winspool.drv", CharSet=CharSet .Unicode,ExactS pelling=true,

            CallingConventi on=CallingConve ntion.StdCall )]

            public static extern long ClosePrinter(In tPtr hPrinter);

            [ DllImport(

            "winspool.d rv" ,CharSet=CharSe t.Auto)]

            public static extern bool GetPrinter(IntP tr hPrinter,int Level,ref
            PRINTERINFO2 pPrinter,int cbBuf,ref int pcbNeeded);

            }

            public class App

            {

            public static void Main ()

            {

            System.IntPtr lhPrinter=new System.IntPtr() ;

            Printer.OpenPri nter("Epson EPL-5700",ref lhPrinter,0);

            PRINTERINFO2 PI=new PRINTERINFO2();

            int Need=0;

            Console.WriteLi ne(Printer.GetP rinter(lhPrinte r,2,ref
            PI,Marshal.Size Of(PI),ref Need));


            Console.WriteLi ne("Error "+Marshal.GetLa stWin32Error()) ;


            Printer.ClosePr inter(lhPrinter );

            Console.ReadLin e();

            }

            }



            Comment

            Working...