473,320 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do i get the printer status

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.
---------------------------------------------------------------
[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 #1
3 9990
Gordon,

Looking into this some more, the correct way to call GetPrinter is to
first make a call to see how big the buffer will be for the call.
Apparently, the pointers in the structure point to the areas right after the
structure itself. Because of this, you will have to take a different
approach.

First, you will have to find out how much memory you need. You do that
with a call to GetPrinter initially:

// Get the number of bytes needed.
Printer.GetPrinter(lhPrinter, 2, IntPtr.Zero, 0, ref Need);

Notice that I changed the declaration of the function so that the third
parameter takes an IntPtr. At this point, Need should have the number of
bytes needed. You can then allocate the memory needed.

// Allocate that in unmanaged memory.
IntPtr pintStruct = Marshal.AllocCoTaskMem(Need);

At this point, you can call GetPrinter again, getting the information:

// Now make the call again and get the bytes in memory.
int SizeOf = Need;
Printer.GetPrinter(lhPrinter, 2, pintStruct, SizeOf, ref Need);

Now you can marshal the structure from unmanaged memory:

// Now marshal the structure manually.
PI = (PRINTERINFO2) Marshal.PtrToStructure(pintStruct,
typeof(PRINTERINFO2));

At this point, all you have to do is free the unmanaged memory:

// Deallocate the memory.
Marshal.FreeCoTaskMem(pintStruct);

Mind you there is no error handling in this code. I'll leave that to
you.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gordon Truslove" <Go*************@QCL-Solutions.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
---------------------------------------------------------------
[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 #2
Great. I'm getting there. You can only check the status when the printer is
trying to print. Otherwise you just get status=0. So i'm working on some
kind of monitoring sysytem now.

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ec**************@TK2MSFTNGP11.phx.gbl...
Gordon,

Looking into this some more, the correct way to call GetPrinter is to
first make a call to see how big the buffer will be for the call.
Apparently, the pointers in the structure point to the areas right after the structure itself. Because of this, you will have to take a different
approach.

First, you will have to find out how much memory you need. You do that with a call to GetPrinter initially:

// Get the number of bytes needed.
Printer.GetPrinter(lhPrinter, 2, IntPtr.Zero, 0, ref Need);

Notice that I changed the declaration of the function so that the third parameter takes an IntPtr. At this point, Need should have the number of
bytes needed. You can then allocate the memory needed.

// Allocate that in unmanaged memory.
IntPtr pintStruct = Marshal.AllocCoTaskMem(Need);

At this point, you can call GetPrinter again, getting the information:

// Now make the call again and get the bytes in memory.
int SizeOf = Need;
Printer.GetPrinter(lhPrinter, 2, pintStruct, SizeOf, ref Need);

Now you can marshal the structure from unmanaged memory:

// Now marshal the structure manually.
PI = (PRINTERINFO2) Marshal.PtrToStructure(pintStruct,
typeof(PRINTERINFO2));

At this point, all you have to do is free the unmanaged memory:

// Deallocate the memory.
Marshal.FreeCoTaskMem(pintStruct);

Mind you there is no error handling in this code. I'll leave that to
you.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gordon Truslove" <Go*************@QCL-Solutions.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
---------------------------------------------------------------
[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 #3
OK if anybody ever needs this. You can check the status by using
job=StartDocPrinter then StartPagePrinter then getprinter which will return
a status code, you can then remove the job usint setjob. This doesn't work
if another app has queued a job before this one.

"Gordon Truslove" <Go*************@QCL-Solutions.com> wrote in message
news:On**************@TK2MSFTNGP11.phx.gbl...
Great. I'm getting there. You can only check the status when the printer is trying to print. Otherwise you just get status=0. So i'm working on some
kind of monitoring sysytem now.

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:ec**************@TK2MSFTNGP11.phx.gbl...
Gordon,

Looking into this some more, the correct way to call GetPrinter is to first make a call to see how big the buffer will be for the call.
Apparently, the pointers in the structure point to the areas right after

the
structure itself. Because of this, you will have to take a different
approach.

First, you will have to find out how much memory you need. You do

that
with a call to GetPrinter initially:

// Get the number of bytes needed.
Printer.GetPrinter(lhPrinter, 2, IntPtr.Zero, 0, ref Need);

Notice that I changed the declaration of the function so that the

third
parameter takes an IntPtr. At this point, Need should have the number of bytes needed. You can then allocate the memory needed.

// Allocate that in unmanaged memory.
IntPtr pintStruct = Marshal.AllocCoTaskMem(Need);

At this point, you can call GetPrinter again, getting the information:
// Now make the call again and get the bytes in memory.
int SizeOf = Need;
Printer.GetPrinter(lhPrinter, 2, pintStruct, SizeOf, ref Need);

Now you can marshal the structure from unmanaged memory:

// Now marshal the structure manually.
PI = (PRINTERINFO2) Marshal.PtrToStructure(pintStruct,
typeof(PRINTERINFO2));

At this point, all you have to do is free the unmanaged memory:

// Deallocate the memory.
Marshal.FreeCoTaskMem(pintStruct);

Mind you there is no error handling in this code. I'll leave that to you.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gordon Truslove" <Go*************@QCL-Solutions.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
---------------------------------------------------------------
[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 #4

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. ...
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.