Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-07-20
6
Medium Priority
?
337 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
[X]
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
  • 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 1500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

688 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