C# Guru's! ReadPrinter....I need to read printer buffer to stringbuilder...

T

trint

The WritePrinter() is working. I know this ReadPrinter()
implementation isn't correct because it never reads into "reaDstrinG"
buffer from the printer...But, I'm sure one of you Guru's do know how I
can make this work (and any help is appreciated):

string reaDstrinG = "";
int pcRead = 0;
for (int i = 0; i < 23; i++)
{
PrintDirect.ReadPrinter(lhPrinter,reaDstrinG,100,ref pcRead);
}


public class PrintDirect: Form1
{

PrintDirect PrDt = new PrintDirect();
public static extern long WritePrinter(IntPtr hPrinter,string data, int
buf,ref int pcWritten);
[ DllImport( "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern long ReadPrinter(IntPtr hPrinter,string data,
int buf,ref int pcRead);
[ DllImport( "winspool.drv"
,CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]

This is very important to me today.
Thanks,
Trint
 
M

Mattias Sjögren

[ DllImport( "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern long ReadPrinter(IntPtr hPrinter,string data,
int buf,ref int pcRead);

Use bool as the return type and make the data parameter a
StringBuilder.



Mattias
 
T

Trint Smith

Mattias,
Do you mean the:
public static extern long ReadPrinter(IntPtr hPrinter,string data,int
buf,ref int pcRead);
should read:
public static extern long ReadPrinter(IntPtr hPrinter,string data,bool
buf,ref bool pcRead);
?
Thanks,
Trint


int buf,ref int pcRead);
Net programmer
(e-mail address removed)
 
M

Mattias Sjögren

Mattias,
Do you mean the:
public static extern long ReadPrinter(IntPtr hPrinter,string data,int
buf,ref int pcRead);
should read:
public static extern long ReadPrinter(IntPtr hPrinter,string data,bool
buf,ref bool pcRead);
?

No, like this

public static extern bool ReadPrinter(IntPtr hPrinter, StringBuilder
data, int buf, out int pcRead);



Mattias
 
T

Trint Smith

Mattias,
I still am having troubles (I guess I don't understand how to get
reading from the printer), although writeprinter is working. Here is my
code that is not showing anything that I know is waiting to be sent from
the printer:
string reaDstrinG = "";
int buf = 0;
int pcRead;

System.Text.StringBuilder sb = new System.Text.StringBuilder();
PrintDirect.OpenPrinter(@"\\Web1\Shipping1",ref lhPrinter,0);
for (int i = -1; i < 23; i++)
{
PrintDirect.ReadPrinter(lhPrinter, sb.Append(reaDstrinG), buf, out
pcRead);
// PrintDirect.ReadPrinter(lhPrinter,reaDstrinG,buffLength,ref
pcRead);
}

Here is what is sent to the printer before this code (this is for a
status report on paper, staples and toner):

st1 = "\x1b%-12345X@PJL \f" +
"@PJL COMMENT ***Inquiring About \f" +
"@PJL COMMENT Environment Settings*** \f" +
@"@PJL ECHO " + formaTtd + " \f" +
"@PJL INQUIRE INTRAY2 \f" +
"@PJL INQUIRE INTRAY3 \f" +
"@PJL INQUIRE INTRAY4 \f" +
"@PJL INQUIRE STAPLER \f" +
"@PJL INQUIRE TONNER \f" +
"@PJL INQUIRE TIMEOUT \f" +
"@PJL ENTER LANGUAGE=PCL \f" +
"\x1b%-12345X\f";

Any help is really appreciated.
Thanks,
Trint

..Net programmer
(e-mail address removed)
 
M

Mattias Sjögren

PrintDirect.ReadPrinter(lhPrinter, sb.Append(reaDstrinG), buf, out
pcRead);

That's a slightly weird call. You can just pass in sb as the second
parameter, you don't have to call Append there.

But the StringBuilder will not magically update reaDstrinG if that's
what you expect. You have to do reaDstrinG = sb.ToString() after the
API call.

If it still doesn't work, I suggest that you start checking the
function's return value.



Mattias
 
Joined
Mar 23, 2010
Messages
1
Reaction score
0
Try this:

[DllImport("winspool.Drv", EntryPoint = "ReadPrinter", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]

public static extern bool ReadPrinter(
IntPtr hPrinter,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder pBytes,
Int32 dwCount,
ref Int32 dwNReadBytes);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top