Solved

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

Posted on 2006-07-20
6
314 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

14 Experts available now in Live!

Get 1:1 Help Now