Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

*Copying* a file direct to printer

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
jdthedj
Asked:
jdthedj
  • 2
  • 2
1 Solution
 
jhunCommented:
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
 
jdthedjAuthor Commented:
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
 
kjtengCommented:
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
 
jhunCommented:
>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
 
jdthedjAuthor Commented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now