Using generic printer

  • Thread starter Thread starter Jean Paul Mertens
  • Start date Start date
J

Jean Paul Mertens

Hello,

Is there a way to send a string of text to a generic tekst printer under
..NET.
Somethings as in the good old days
File f = Open("LPT1");
f.Writeline("Blablabla");

The goal is to use an old lineprinter as a log printer printing out each
line (incomming allert) at a time without having to build a whole page
before printing.

Greets,

Jean Paul
 
Jean Paul,

What you can do is call the CreateFile API function through the P/Invoke
layer. This will allow you to specify "LPT1", and give you a handle to that
printer.

Then, you can pass that handle to the FileStream class and use that
instance to write to the port.

Hope this helps.
 
Nicholas,

I'l give it a try.

tnx
JP

Nicholas Paldino said:
Jean Paul,

What you can do is call the CreateFile API function through the
P/Invoke layer. This will allow you to specify "LPT1", and give you a
handle to that printer.

Then, you can pass that handle to the FileStream class and use that
instance to write to the port.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jean Paul Mertens said:
Hello,

Is there a way to send a string of text to a generic tekst printer under
.NET.
Somethings as in the good old days
File f = Open("LPT1");
f.Writeline("Blablabla");

The goal is to use an old lineprinter as a log printer printing out each
line (incomming allert) at a time without having to build a whole page
before printing.

Greets,

Jean Paul
 
It is certainly possible, although it is not necessarily advisable. Printers
are accessed via their drivers, and for good reason. Assuming that you have
an old dot matricx printer that works, and it can print out text sent to it
directly, you can certainly open the LPT port and write to it.

However, as I said, that's not necessarily the best idea. It's hard to know
without knowing your actual requirements. Your concept of logging is rather
bizarre. A long time ago, logging was done this way, because it was the only
way possible. But putting data onto paper is both expensive in terms of
resources used (paper, ink, etc), and processor overhead (because printing
is a high priority process). In addition, paper has its purposes, but
storage of information is hardly one of them. You can store beaucoups more
information in a much smaller (and more reliable) form on a computer, a
disk, or a CD/DVD. If you do not want to store it, why are you printing it
to a permanent (and large) storage medium?

Can you provide some more of the reasons behind this odd approach?

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Expect the unaccepted.
 
Hey Kevin,

One of the main reasons to log on paper is the juridical value of the paper.
We are a garage and by example the law is still requesting that the selling
of gas oil has to be registered in real time directly on paper. Also the
entrance and the leave of a car in the garage has to be registered on a
paper carrier.



An other good reason to log on paper is, if you log only important events on
the paper, that it is more fasten to control. If I log my backup events in
the application logbook it is certainly fine but most of the time my backup
runs without any problem. On the server there are so many application events
that most of the time one only inspect this logbook when there is really a
known problem. I have five servers running and so real problem events can
hide in the mass. If now there is a real problem with a backup on one of the
machines and I send this event to the log printer it just need a quick
inspect in the morning and if I see an error message between the lines of
selling gas oil and registering the cars, it is seen directly and can be
corrected.



And a third reason is that one can easy centralize debug information. It's a
habit I inherited from my VAX time writing in Fortran and Assembler having
merely no debuggers (we only had crashdumps, if you ever analyzed one you
know what I mean). So when an error or a certain event occurs and you can
send this to a printer from wherever on the network, it reconstructs better
and helps on tracking problems.



If you restrict to one line logging and only relevant items you nearly use
one box of paper a year. So that's not a reason to not doing it.



About storing logs to a digital medium; can you still read 8" disks... so I
have to go to the museum for inspecting my logs what with CD's in ten or
fifty years... but I can still read the papers of the middle ages. And I am
sure that those people would wonder why one should, for God shake, be
interested a 1,000 years later in what they wrote on a sheet of paper.



CU
JP
 
Good enough, Jean Paul!

At any rate, I see that your question has been more specifically answered
already. Let me know if you need any more specific information.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Expect the unaccepted.
 
To all,

If ever one is interested doing the same here is my solution

Greets and succes

JP

[Code snipset...]
public class ErrorPrinter : MarshalByRefObject

{

public ErrorPrinter()

{

}

const uint GENERIC_READ = 0x80000000;

const uint GENERIC_WRITE = 0x40000000;

const uint OPEN_EXISTING = 3;

[DllImport("kernel32.dll", SetLastError = true)]

static extern SafeFileHandle CreateFile(string lpFileName, uint
dwDesiredAccess,

uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,

uint dwFlagsAndAttributes, IntPtr hTemplateFile);


public void LPrint(string msg)

{

Console.WriteLine("Message is: {0}", msg);

SafeFileHandle handleValue = null;

string toPrint = string.Format("{0:dd/MM/yy hh:mm:ss}: {1}\n",DateTime.Now,
msg);

// open LPT1 for Writing

handleValue = CreateFile("LPT1:", GENERIC_WRITE, 0, IntPtr.Zero,
OPEN_EXISTING, 0, IntPtr.Zero);


if (handleValue.IsInvalid)

return ;


FileStream f = new FileStream(handleValue, FileAccess.Write);

byte[] buffer = Encoding.ASCII.GetBytes(toPrint);

f.Write(buffer, 0, buffer.Length);

f.Flush();

f.Close();

}

}

[End Code Snipset...]
 
Nice work! Tres bien!

--

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Accept the Unexpected.

Jean Paul Mertens said:
To all,

If ever one is interested doing the same here is my solution

Greets and succes

JP

[Code snipset...]
public class ErrorPrinter : MarshalByRefObject

{

public ErrorPrinter()

{

}

const uint GENERIC_READ = 0x80000000;

const uint GENERIC_WRITE = 0x40000000;

const uint OPEN_EXISTING = 3;

[DllImport("kernel32.dll", SetLastError = true)]

static extern SafeFileHandle CreateFile(string lpFileName, uint
dwDesiredAccess,

uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,

uint dwFlagsAndAttributes, IntPtr hTemplateFile);


public void LPrint(string msg)

{

Console.WriteLine("Message is: {0}", msg);

SafeFileHandle handleValue = null;

string toPrint = string.Format("{0:dd/MM/yy hh:mm:ss}:
{1}\n",DateTime.Now, msg);

// open LPT1 for Writing

handleValue = CreateFile("LPT1:", GENERIC_WRITE, 0, IntPtr.Zero,
OPEN_EXISTING, 0, IntPtr.Zero);


if (handleValue.IsInvalid)

return ;


FileStream f = new FileStream(handleValue, FileAccess.Write);

byte[] buffer = Encoding.ASCII.GetBytes(toPrint);

f.Write(buffer, 0, buffer.Length);

f.Flush();

f.Close();

}

}

[End Code Snipset...]


Kevin Spencer said:
Good enough, Jean Paul!

At any rate, I see that your question has been more specifically answered
already. Let me know if you need any more specific information.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Expect the unaccepted.
 
Back
Top