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
Solved

Printing binary files to network printer: Can I use a dynamic array for BlockRead

Posted on 2006-07-20
6
324 Views
Last Modified: 2010-04-05
I need to be able to send saved binary report files to a network printer but found that the only way to fill the buffer in BlockRead (to use in the WritePrinter function) is to use a static array. This is very limiting as I have no way of knowing in advance how big the print file is going to be. Too large and it will run out of array space, too small and it's wasting memory.

The question is, is there any way of using a dynamic array instead or some other clever way of assigning a size to a static array at runtime? I've already tried one suggestion using a dynamic array such as: BlockRead(PrnFile, Buffer[0], SizeOf(Buffer)) but this never prints the file properly.

My code is as follows:

procedure TReporter.PrintBinaryFile(binFile: string); //this is the passed UNC network path + file name
var hPrinter: THANDLE;
    DocInfo:  T_DOCINFO_1;
    BytesWritten: DWORD;
    PrnFile: file;
    Buffer: array[1..4096] of char; //the static array I'd like to change at runtime
    Device: array[0..255] of char;
    Driver: array[0..255] of char;
    Port: array[0..255] of char;
    hDMode: THandle;
    b: integer; //loop var
    fs: integer; //file size used to find end of binary report data in array
begin
  Printer.PrinterIndex := -1; //default printer
  Printer.GetPrinter(Device, Driver, Port, hDMode);
  if not (OpenPrinter(Device, hprinter, nil)) then
    MessageDlg('Can''t open printer to print report file!', mtError, [mbOK], 0)
  else
  begin
    AssignFile(PrnFile, binFile);
    Reset(PrnFile, 1);
    fs := FileSize(PrnFile) + 1; //marks position of end of file data
    //read contents of print file into buffer
    BlockRead(PrnFile, Buffer, SizeOf(Buffer));
    //write null values to end of unused buffer to prevent rogue data from printing
    for b := fs to Length(Buffer) do
      Buffer[b] := Chr(0);
    CloseFile(PrnFile);
    DocInfo.pDocName := PChar(binFile);
    DocInfo.pOutputFile := nil;
    DocInfo.pDataType := 'RAW';
    StartDocPrinterA(hPrinter, 1, @DocInfo);
    StartPagePrinter(hPrinter);
    WritePrinter(hPrinter, @Buffer, SizeOf(Buffer), BytesWritten);
    EndPagePrinter(hPrinter);
    EndDocPrinter(hPrinter);
    ClosePrinter(hPrinter);
    //tidy up and delete print file when printed
    DeleteFile(binFile);
  end;
end;

This problem has perplexed me for two days now, so I'm awarding good points for a solution. Thanks in advance.
0
Comment
Question by:epawlik
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17146852
Use a TFileStream to open the file and just fill up the buffer from the FileStream...like FileStream.Write( Buffer, 4096);
0
 

Author Comment

by:epawlik
ID: 17147339
Unfortunately I'm still stuck with specifying the buffer size by doing this. I need to dynamically get the file size and either assign this size to a static array at runtime or find some way of assigning the size to a dynamic array, but as I've said, the dynamic array Buffer won't work correctly; it either prints nothing at all, or prints gobbledygook. Thanks for your interest.
0
 
LVL 6

Accepted Solution

by:
House_of_Dexter earned 500 total points
ID: 17147421
The problem with your code is not that you are using a static array...is that you are only doing ONE blockread...TFileStream will handle loading the file into memory so that you can read it...

OR...

repeat
   BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
   //work with Block here...
 until (NumRead = 0);
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:epawlik
ID: 17152511
Thanks again for your help House_of_Dexter. You've certainly pointed me in the right direction and it's made the code neater by using a TFileStream object and as you say, the static array isn't so important then, in fact I've reduced the size to 2048. I was still getting a page of gobbledygook but then realised that the Buffer array required initialising with null values on each iteration, so for the benefit of others here is the revised code:

procedure TReporter.PrintBinaryFile(binFile: string);
var FileStream: TFileStream;
    hPrinter: THANDLE;
    DocInfo:  T_DOCINFO_1;
    BytesWritten, BytesRead: DWORD;
    Buffer: array[1..2048] of char;
    Device: array[0..255] of char;
    Driver: array[0..255] of char;
    Port: array[0..255] of char;
    hDMode: THandle;
    b: integer; //loop var
begin
  Printer.PrinterIndex := -1; //default printer
  Printer.GetPrinter(Device, Driver, Port, hDMode);
  if not (OpenPrinter(Device, hprinter, nil)) then
    MessageDlg('Can''t open printer to print report file!', mtError, [mbOK], 0)
  else
  begin
    FileStream := TFileStream.Create(binFile, fmOpenRead); //nice neat method of reading in file data
    DocInfo.pDocName := PChar(binFile);
    DocInfo.pOutputFile := nil;
    DocInfo.pDataType := 'RAW';
    StartDocPrinterA(hPrinter, 1, @DocInfo);
    StartPagePrinter(hPrinter);
    repeat
      for b := 1 to 2048 do
        Buffer[b] := Chr(0); //initialise the Buffer array (if you don't you get trash printing at the end of the print file)
      //read contents of printer file into buffer
      BytesRead := FileStream.Read(Buffer, 2048); //Read returns number of bytes read from file
      if BytesRead > 0 then
        WritePrinter(hPrinter, @Buffer, SizeOf(Buffer), BytesWritten);
    until BytesRead = 0; //end of print file has been reached
    EndPagePrinter(hPrinter);
    EndDocPrinter(hPrinter);
    ClosePrinter(hPrinter);
    FileStream.Free;
    //tidy up and delete print file when printed
    DeleteFile(binFile);
  end;
end;
0
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17153934
great...when you have to deal with a buffer in any shape or form...always look at TStream...there are so many cool things that interface with them like StringList and so on...
0
 

Author Comment

by:epawlik
ID: 17154014
Thanks very much for the tip. Much appreciated.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 149
delphi exception 7 68
code issue 8 152
Error E2158 compiling with Delphi XE10 Seattle 2 112
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

792 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