Solved

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

Posted on 2006-07-20
6
320 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

772 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