views:

143

answers:

3

Hi,

I need to do some printing using TPrinter. The problem is I can not, for various reasons, use Global object Printer.

I want to be able to create my instance of TPrinter and print using that one.

I tried with:

MyPrinter := TPrinter.Create;
MyPrinter.BeginDoc;

but this generates AV.

Any idea what does it take to print something using my instance of TPrinter?

Regards Goran Nagy

+1  A: 

If you look at the soucre for printers, the AbortProc uses the global FPrinter object.
You can solve this by call Printer function before caling TPrinter.Create, then it dosn't generate a AccessViolation. It will posibly solve your problem, but MyPrinter.Abort wil NOT propoply not work correcty.

Telling why you can't use the global object, gives other users a posibility to suggest alternative solutions.

BennyBechDk
+1  A: 

The TPrinter object is not really designed to be created locally, but instead is designed to be used from the 'singleton' function Printer in the Printers unit. Generally you would use that.

From the help:

Use TPrinter to manage any printing performed by an application. Obtain an instance of TPrinter by calling the Printer function in the Printers unit.

Edit: Actually, having had a think you could do something like this:

procedure PrintThings;
var
  LMyPrinter: TPrinter;
  LOldPrinter: TPrinter;
begin
  LMyPrinter := TPrinter.Create;
  try
    LOldPrinter := SetPrinter(LMyPrinter);
    try

      // your printing code goes here

    finally
      SetPrinter(LOldPrinter);
    end;
  finally
    LMyPrinter.Free;
  end;
end;
Nat
A: 

Yes, I am printing multiple things at the same time using different printers.

Suppose I have main thread that does some printing, and then I have this custom hardware unit attached to PC. On some event this unit will tell my PC to start another print job. This happens in the main thread and current print job is "paused" because main thread receives event from my component that reads data from this hardware unit. I can not just abort previous print job, and of course I cant use global Printer to start new job.

This is reason why I can't use what Nat suggested.

I could of course have some print queue in my application, but it's not really what I want to do (again there are some reasons for that).

I just need possibility to print to 2 different printers at the same time.

Are there any alternatives to TPrinter (commercial or freeware)?

Thanks Goran

gorann