473,466 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Printer Status

Has anybody managed to get GetPrinter and PRINER_INFO_2 to work?
I would like to retrieve the printer status.
Nov 15 '05 #1
5 6187
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)paldino=at=exisconsulting<dot>com

"Gordon Truslove" <Go*************@QCL-Solutions.com> wrote in message
news:OS**************@TK2MSFTNGP10.phx.gbl...
Has anybody managed to get GetPrinter and PRINER_INFO_2 to work?
I would like to retrieve the printer status.

Nov 15 '05 #2
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("winspool.drv" )]

public static extern bool GetPrinter(IntPtr hPrinter,long Level,ref
PRINTERINFO2 pPrinter,long cbBuf,ref long pcbNeeded);
[StructLayout( LayoutKind.Sequential)]

public struct PRINTERINFO2

{

[MarshalAs(UnmanagedType.LPWStr)]public string pServerName;

[MarshalAs(UnmanagedType.LPWStr)]public string pPrinterName;

[MarshalAs(UnmanagedType.LPWStr)]public string pShareName;

[MarshalAs(UnmanagedType.LPWStr)]public string pPortName;

[MarshalAs(UnmanagedType.LPWStr)]public string pDriverName;

[MarshalAs(UnmanagedType.LPWStr)]public string pComment;

[MarshalAs(UnmanagedType.LPWStr)]public string pLocation;

public DEVMODE pDevMode;

[MarshalAs(UnmanagedType.LPWStr)]public string pSepFile;

[MarshalAs(UnmanagedType.LPWStr)]public string pPrintProcessor;

[MarshalAs(UnmanagedType.LPWStr)]public string pDatatype;

[MarshalAs(UnmanagedType.LPWStr)]public string pParameters;

public SECURITY_DESCRIPTOR pSecurityDescriptor;

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.Sequential)]

public struct SECURITY_DESCRIPTOR{

public byte Revision;

public byte Sbz1;

public long Control;

public long Owner;

public long Group;

public ACL sACL;

public ACL Dacl;

}

[StructLayout( LayoutKind.Sequential)]

public struct ACL{

public byte AclRevision;

public byte Sbz1;

public int AclSize;

public int AceCount;

public int Sbz2;

}

[StructLayout( LayoutKind.Sequential)]

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 dmDisplayFrequency;

public long dmICMMethod;

public long dmICMIntent;

public long dmMediaType;

public long dmDitherType;

public long dmReserved1;

public long dmReserved2;

}

"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:%2****************@TK2MSFTNGP10.phx.gbl...
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)paldino=at=exisconsulting<dot>com

"Gordon Truslove" <Go*************@QCL-Solutions.com> wrote in message
news:OS**************@TK2MSFTNGP10.phx.gbl...
Has anybody managed to get GetPrinter and PRINER_INFO_2 to work?
I would like to retrieve the printer status.


Nov 15 '05 #3
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_DESCRIPTOR 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)paldino=at=exisconsulting<dot>com
"Gordon Truslove" <Go*************@QCL-Solutions.com> wrote in message
news:uz**************@tk2msftngp13.phx.gbl...
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("winspool.drv" )]

public static extern bool GetPrinter(IntPtr hPrinter,long Level,ref
PRINTERINFO2 pPrinter,long cbBuf,ref long pcbNeeded);
[StructLayout( LayoutKind.Sequential)]

public struct PRINTERINFO2

{

[MarshalAs(UnmanagedType.LPWStr)]public string pServerName;

[MarshalAs(UnmanagedType.LPWStr)]public string pPrinterName;

[MarshalAs(UnmanagedType.LPWStr)]public string pShareName;

[MarshalAs(UnmanagedType.LPWStr)]public string pPortName;

[MarshalAs(UnmanagedType.LPWStr)]public string pDriverName;

[MarshalAs(UnmanagedType.LPWStr)]public string pComment;

[MarshalAs(UnmanagedType.LPWStr)]public string pLocation;

public DEVMODE pDevMode;

[MarshalAs(UnmanagedType.LPWStr)]public string pSepFile;

[MarshalAs(UnmanagedType.LPWStr)]public string pPrintProcessor;

[MarshalAs(UnmanagedType.LPWStr)]public string pDatatype;

[MarshalAs(UnmanagedType.LPWStr)]public string pParameters;

public SECURITY_DESCRIPTOR pSecurityDescriptor;

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.Sequential)]

public struct SECURITY_DESCRIPTOR{

public byte Revision;

public byte Sbz1;

public long Control;

public long Owner;

public long Group;

public ACL sACL;

public ACL Dacl;

}

[StructLayout( LayoutKind.Sequential)]

public struct ACL{

public byte AclRevision;

public byte Sbz1;

public int AclSize;

public int AceCount;

public int Sbz2;

}

[StructLayout( LayoutKind.Sequential)]

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 dmDisplayFrequency;

public long dmICMMethod;

public long dmICMIntent;

public long dmMediaType;

public long dmDitherType;

public long dmReserved1;

public long dmReserved2;

}

"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
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)paldino=at=exisconsulting<dot>com

"Gordon Truslove" <Go*************@QCL-Solutions.com> wrote in message
news:OS**************@TK2MSFTNGP10.phx.gbl...
Has anybody managed to get GetPrinter and PRINER_INFO_2 to work?
I would like to retrieve the printer status.



Nov 15 '05 #4
Well I'm getting closer. ifferent error this time

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

[StructLayout( LayoutKind.Sequential,CharSet=CharSet.Auto)]

public struct PRINTERINFO2

{

[MarshalAs(UnmanagedType.LPTStr)]public string pServerName;

[MarshalAs(UnmanagedType.LPTStr)]public string pPrinterName;

[MarshalAs(UnmanagedType.LPTStr)]public string pShareName;

[MarshalAs(UnmanagedType.LPTStr)]public string pPortName;

[MarshalAs(UnmanagedType.LPTStr)]public string pDriverName;

[MarshalAs(UnmanagedType.LPTStr)]public string pComment;

[MarshalAs(UnmanagedType.LPTStr)]public string pLocation;

public IntPtr pDevMode;

[MarshalAs(UnmanagedType.LPTStr)]public string pSepFile;

[MarshalAs(UnmanagedType.LPTStr)]public string pPrintProcessor;

[MarshalAs(UnmanagedType.LPTStr)]public string pDatatype;

[MarshalAs(UnmanagedType.LPTStr)]public string pParameters;

public IntPtr pSecurityDescriptor;

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,ExactSpelli ng=false,

CallingConvention=CallingConvention.StdCall )]

public static extern long OpenPrinter(string pPrinterName,ref IntPtr
phPrinter, int pDefault);

[ DllImport(

"winspool.drv",CharSet=CharSet.Unicode,ExactSpelli ng=true,

CallingConvention=CallingConvention.StdCall )]

public static extern long ClosePrinter(IntPtr hPrinter);

[ DllImport(

"winspool.drv" ,CharSet=CharSet.Auto)]

public static extern bool GetPrinter(IntPtr 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.OpenPrinter("Epson EPL-5700",ref lhPrinter,0);

PRINTERINFO2 PI=new PRINTERINFO2();

int Need=0;

Console.WriteLine(Printer.GetPrinter(lhPrinter,2,r ef
PI,Marshal.SizeOf(PI),ref Need));
Console.WriteLine("Error "+Marshal.GetLastWin32Error());
Printer.ClosePrinter(lhPrinter);

Console.ReadLine();

}

}
Nov 15 '05 #5
Well I'm getting closer. different error this time

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

[StructLayout( LayoutKind.Sequential,CharSet=CharSet.Auto)]

public struct PRINTERINFO2

{

[MarshalAs(UnmanagedType.LPTStr)]public string pServerName;

[MarshalAs(UnmanagedType.LPTStr)]public string pPrinterName;

[MarshalAs(UnmanagedType.LPTStr)]public string pShareName;

[MarshalAs(UnmanagedType.LPTStr)]public string pPortName;

[MarshalAs(UnmanagedType.LPTStr)]public string pDriverName;

[MarshalAs(UnmanagedType.LPTStr)]public string pComment;

[MarshalAs(UnmanagedType.LPTStr)]public string pLocation;

public IntPtr pDevMode;

[MarshalAs(UnmanagedType.LPTStr)]public string pSepFile;

[MarshalAs(UnmanagedType.LPTStr)]public string pPrintProcessor;

[MarshalAs(UnmanagedType.LPTStr)]public string pDatatype;

[MarshalAs(UnmanagedType.LPTStr)]public string pParameters;

public IntPtr pSecurityDescriptor;

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,ExactSpelli ng=false,

CallingConvention=CallingConvention.StdCall )]

public static extern long OpenPrinter(string pPrinterName,ref IntPtr
phPrinter, int pDefault);

[ DllImport(

"winspool.drv",CharSet=CharSet.Unicode,ExactSpelli ng=true,

CallingConvention=CallingConvention.StdCall )]

public static extern long ClosePrinter(IntPtr hPrinter);

[ DllImport(

"winspool.drv" ,CharSet=CharSet.Auto)]

public static extern bool GetPrinter(IntPtr 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.OpenPrinter("Epson EPL-5700",ref lhPrinter,0);

PRINTERINFO2 PI=new PRINTERINFO2();

int Need=0;

Console.WriteLine(Printer.GetPrinter(lhPrinter,2,r ef
PI,Marshal.SizeOf(PI),ref Need));
Console.WriteLine("Error "+Marshal.GetLastWin32Error());
Printer.ClosePrinter(lhPrinter);

Console.ReadLine();

}

}

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: rbt | last post by:
Hello there, Depending on the firmware version of the HP printer and the model type, one will encounter a myriad of combinations of the following strings while reading the index page: hp HP...
7
by: Jim Warner | last post by:
I am having problems using Borland 5.02 C/C++ for testing printer status repeatly. I wish to use my printer to detect off-line, removing paper, and so on with my software. I have tried using...
0
by: Gordon Truslove | last post by:
I've been trying to get the printer status using GetPrinter and Printer_Info_2 I'm getting closer, but it still fails. Error 122 - The data area passed to a system call is too small. ...
3
by: Gordon Truslove | last post by:
I've been trying to get the printer status using GetPrinter and Printer_Info_2 I'm getting closer, but it still fails. Error 122 - The data area passed to a system call is too small....
0
by: cortukmehmet | last post by:
I wrote this code to check the status of printer.But it says "Unknown" as printer. /////////////////////////////////////////////////////////////////////////////...
1
by: Vanessa | last post by:
Hi, I'm trying to select a printer in the system printers collection. I'm able to do this. However, if the printer is a network printer and happens that this printer is not on or not ready, I...
6
by: Pipo | last post by:
Hi, Does anyone know how I can read out the printer status? I want to sent a bulk of documents to the printer (or 1 by 1) but if something goes wrong e.g. out of paper, cartridge empty, tray...
2
by: TARUN | last post by:
Hello all, Please help and suggest the code to get the printer status over the network. for Example, i have an string "\\\\os1\\PtName" where os1 is the system name and PtName is the printer...
9
by: id10t error | last post by:
Hello, I am going to be using a Symbol WT4090 to scan items. I need to printer a tag from the Zebra ql320 plus. I am trying to do this is Visual basic 2005. Does anyone know and good site to...
1
by: Steve | last post by:
Hi All I am using vb.net 2005 in a windows forms application I send data to the selected windows printer using a PrintDocument object Is there any way to detect if the Printer is not...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.