Solved

*Copying* a file direct to printer

Posted on 1998-06-03
5
229 Views
Last Modified: 2010-04-06
Hi

I am using D1 with WFW 3.11.

I need to be able to copy files that have been saved as "Print to File", directly to the Printer.

On my form I have an OpenDialog and a PrintDialog, and the user can select multiple files.  Originally I adapted the CopyFile routine from the FMXUtils unit and set the destination to LPT1.  That worked fine.  However the main user of the ap has asked that the program be changed to allow access to different printers.

The PrintDialog gives me the "long" name of the selected printer which I can access from the TPrinter object, but my question is, what do I set the destination name to in the CopyFile routine?
0
Comment
Question by:jdthedj
  • 2
  • 2
5 Comments
 

Accepted Solution

by:
jhun earned 100 total points
ID: 1349984
Here is a code fragment that should help you. (This works only under windows 16 bit !)

unit Esc1;

interface

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

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

var
  Form1: TForm1;

implementation

{ add the printers unit }
uses
   Printers;

{$R *.DFM}

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


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

{ Test to see if the "PASSTHROUGH" escape is supported }
  TestInt := PASSTHROUGH;
  if Escape(Printer.Handle,
            QUERYESCSUPPORT,
            sizeof(TestInt),
            @TestInt,
            nil) > 0 then begin

  { Start the printout }
    Printer.BeginDoc;

  { 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}
    Escape(Printer.Canvas.Handle,
           PASSTHROUGH,
           0,
           @Buff,
           nil);

  { End the printout }
    Printer.EndDoc;
  end;
end;

end.

The printer is selected by the printerindex property !
0
 
LVL 3

Author Comment

by:jdthedj
ID: 1349985
Hi JHun - thanks for the reply.  It doesn't seem to want to work for me, but I can see the general principle behind it.  Also, the environment in which it will need to work is Windows 95/98/NT as well as 3.11.  The files to be sent to the printer are plotter files - not text files.

I was wondering if there was any way of copying the file to the printer canvas in Delphi (by reading in chunks of it at a time).
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1349986
The old dos command: "copy/b xxx.prn lpt1" may be useful for you. To call copy command in your code, use shellExecute as follows:

 strPcopy( p, ' /c copy/b  prnfile.prn lpt1');
 w:= ShellExecute( handle, 'open',
                   'command.com',
                   p,
                   'c:\',
                   sw_showminimized);

{assuming that you have command.com in c:\ and p is declared as a pchar or zerobased array. }

This may not work for 32 bits delphi. I think the equivalent API in d2/d3 is createProcess. Please check the API help if necessary.

BTW, have I tried copyfile('c:\a.prn', 'lpt1'), it does not seems to work on my pc/printer. Any idea of the reason?

0
 

Expert Comment

by:jhun
ID: 1349987
>Hi JHun - thanks for the reply.  It doesn't seem to want to >work for me, but I can see the general principle behind it.  
>Also, the environment in which it will need to work is Windows >95/98/NT as well as 3.11.  The files to be sent to the printer >are plotter files - not text files.
Each envirement has to be programmed separadly ! The code for windows 95 is different. Also microsoft has announced that passtrough support is obsolete.


>I was wondering if there was any way of copying the file to the >printer canvas in Delphi (by reading in chunks of it at a time).
When you print to the canvas you get the printerreset etc. also send to the printer, but that code already included in your file.

Gr. John
0
 
LVL 3

Author Comment

by:jdthedj
ID: 1349988
Thanks for the replies guys.  With a bit of fiddling using the abovementioned CopyFile routine I have overcome the WIN95 problem.  I was able to map the printers (on the network) to LPT1.  For WINNT there is a file called Print.exe which has a parameter for a network printer, so it looks as though I have got the job done.

JHun - although your answer didn't actually solve my problem it answered another problem I had so I am giving you the points.

KJTeng - I don't know why you couldn't get the CopyFile routine to work for you.  Maybe it's because I am printing graphics files, not test files.

Thanks again for all the help to both of you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now