Solved

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

Posted on 2006-07-20
6
317 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
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.

 

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

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

Suggested Solutions

Title # Comments Views Activity
calling Tcolordialog in dll make the form disappear 8 103
Delphi : could not find program, '...exe' 2 147
find a node in VST 2 63
drawing animated level bar based on numbers 3 88
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

15 Experts available now in Live!

Get 1:1 Help Now