473,396 Members | 2,037 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,396 software developers and data experts.

Using generic printer

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
Aug 8 '06 #1
7 2019
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]
- mv*@spam.guard.caspershouse.com

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:eo**************@TK2MSFTNGP03.phx.gbl...
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

Aug 8 '06 #2
Nicholas,

I'l give it a try.

tnx
JP

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comschreef
in bericht news:Ow****************@TK2MSFTNGP05.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:eo**************@TK2MSFTNGP03.phx.gbl...
>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


Aug 8 '06 #3
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.

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:eo**************@TK2MSFTNGP03.phx.gbl...
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

Aug 8 '06 #4
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

"Kevin Spencer" <uc*@ftc.govschreef in bericht
news:%2***************@TK2MSFTNGP05.phx.gbl...
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.

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:eo**************@TK2MSFTNGP03.phx.gbl...
>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


Aug 9 '06 #5
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.

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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

"Kevin Spencer" <uc*@ftc.govschreef in bericht
news:%2***************@TK2MSFTNGP05.phx.gbl...
>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.

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:eo**************@TK2MSFTNGP03.phx.gbl...
>>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



Aug 9 '06 #6
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" <uc*@ftc.govschreef in bericht
news:uL**************@TK2MSFTNGP03.phx.gbl...
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.

Aug 9 '06 #7
Nice work! Tres bien!

--

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Accept the Unexpected.

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:OY*************@TK2MSFTNGP05.phx.gbl...
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" <uc*@ftc.govschreef in bericht
news:uL**************@TK2MSFTNGP03.phx.gbl...
>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.


Aug 9 '06 #8

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

Similar topics

0
by: a_man | last post by:
Hi everyone Im writing an application in c# and I want to print directly to a printer with some printer specific commands than only the printer understands. Below is my code. I have an arraylist...
13
by: Susan Beebe | last post by:
I have downloaded the code described in Microsoft article Q154078. I am trying to send raw ZPL (zebra barcode printer) code from Microsoft access. It works just fine if I hard code the actual...
1
by: hasanainf | last post by:
Hi all and thanking you all in advance for your help My client wants to use a receipt printer. Since I have never used one before I have some questions. 1. Unlike other printers which are set...
7
by: Jean Paul Mertens | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.