how to send escape codes to the printer?

Posted on 1998-05-17
Last Modified: 2012-06-22

a friend of mine has to work with a printer that uses the generic Windows printer driver. To change font attributes like 'bold' he has to send an escape sequence to the printer. However, the printer driver replaces the non-printable characters by '@', making such changes impossible. Now there are two questions:

1- is there a 'neutral' windows printer driver available wich does not filter out the escapes?
2- (if 1 does not work) he tried to use windows API calls like ExtEscape, but could not get them to work. How could he use the TPrinter object and mix this with ExtEscape?

I know he could write directly to the printer port, but ofcourse it would be preferable to use as much Delphi + windows as possible.


Question by:cadenza
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Accepted Solution

ZifNab earned 200 total points
ID: 1346043
Hi cadenza, directly from borland technical information :

3196:Direct Commands to Printer - Passthrough/Escape

KEYWORDS: passthrough, escape, printers AREA: General

Although Delphi's TPrinter unit makes it easy to interface
to a given printer, there are times when you may need
to drop down to the printers level and send device specific
escape sequences. Under 16-bit versions of Windows, this
was as easy as opening the printer port, but under Windows NT,
directly accessing the hardware is is illegal. One solution
is to use the Windows "PASSTHROUGH" escape to send an escape
sequence directly to the printer. In order to use the
"PASSTHROUGH" escape, it must be supported by the printer
driver. Be forwarned that not all printer drivers will support
this feature.

It's worth noting that the "PASSTHROUGH" escape is documented
as obsolete for thirty-two bit applications. It should be a
number of years before this escape goes by the way, since
it is used in many commercial applications.

The example code presented is not targeted to any specific
printer model. You will need to know the correct escape
sequences to send to the printer you are interfacing to.
Note that you must still call the BeginDoc and EndDoc methods
of TPrinter. During the BeginDoc call, the printer driver
initializes the printer as necessary, and during the EndDoc
call, the printer driver will uninitialize the printer and
eject the page. When you do make your escape call, the printer
may be set for the current windows mapping mode if the printer
supports scaling internaly. Technically, you should not do
anything that would cause the printer memory to be reset,
or eject a page with an escape sequence. In other words,
try to leave the printer in the same state it was in when
you made your escape. This is more important on intellegent
printers (Postscript printers), and not important at all on
a standard TTY line printer, where you can do just about
anything you like, including ejecting pages.

Code Example:

You will need to declare a structure to hold the buffer you are
sending. The structure of the buffer is defined as a word containing
the length of the buffer, followed by the buffer containing the data.

Before making the escape call to pass the data, we will use
the escape "QUERYESCSUPPORT" to determine if the "PASSTHROUGH"
escape is supported by the print driver.

Finally, be aware that your data will be inserted directly into
the printers data stream. On some printer models (Postscript),
you may need to add a space to the start and end of your data
to separate your data from the printer drivers data.

(Postscript is a Registered Trademark of Adobe Systems Incorporated)

unit Esc1;


  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    { Private declarations }
    { Public declarations }

  Form1: TForm1;


{ add the printers unit }

{$R *.DFM}

{ declare the "PASSTHROUGH" structure }
type TPrnBuffRec = record
  BuffLength : word;
  Buffer : array [0..255] of char;

procedure TForm1.Button1Click(Sender: TObject);
  Buff : TPrnBuffRec;
  TestInt : integer;
  s : string;

{ Test to see if the "PASSTHROUGH" escape is supported }
  if Escape(Printer.Handle,
            nil) > 0 then begin

  { Start the printout }

  { Make a string to passthrough }
    s := ' A Test String ';

  { Copy the string to the buffer }
    StrPCopy(Buff.Buffer, s);

  { Set the buffer length }
    Buff.BuffLength := StrLen(Buff.Buffer);

  { Make the escape}

  { End the printout }



Regards, ZiF.

Author Comment

ID: 1346044
Hi ZifNab,

Jules Verne let one of his characters say:

"use what you've got, so that you won't need what you've not"

and this seems to hold for us too! I passed your answer to my friend and he is testing it at the moment. I think that if it does not work for him (because maybe the generic windows printer driver does not have this capability ?) he is out of luck. I'll be back soon,


Expert Comment

ID: 1346045
If you still have problems sending code directly to the printer, simle solution I've been using lattely is to write text to a temporary text file and copy file to the LPT at the end of the printing job.
It works perfectly with Delphi 2.0 and NT 4.0.

Author Comment

ID: 1346046

The generic windows printer driver does not have the PASSTHROUGH capability, so I am afraid that is not the way to go. Strange, though, since this driver is the one most likely to need passthrough!!


Thanks. I'll pass your answer to my friend and will be back soon.


Author Comment

ID: 1346047
ZifNab: you answered the question, and although it did not give the definitive solution, I'll give you the points.

Smico: thanks for your suggestion. It amounts more or less to writing to the port directly, a thing we wanted to avoid, and was implied in the question already. Unfortunately I don't see a way to give you points too. Sorry.


Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question