?
Solved

Setting output printer

Posted on 2004-04-21
4
Medium Priority
?
1,159 Views
Last Modified: 2010-08-05
Hi,

I am making my own printDialog and want to set the output printer in Delphi. I am working on a Win2000 system. Here's an example of my code:

procedure TMyPrintDlg.ChangePrinter(PrinterName : pchar);
var
  SpaceNeeded : word; // for pDriverinfo2
  pDBuff : _devicemodeA; // dummy
  devmode : PDeviceModeA;
  Driver_info_2 : pDriverinfo2;
  DevModeSize : integer;
  dPtr : Pointer;
  hPrinter : THandle;
begin
   // Get the printer handle
   OpenPrinter(Printername, hPrinter, nil);
   // find out how much space we need for the info stuct
   GetPrinter(hPrinter, 2, 0, 0, @SpaceNeeded);
   // allocate the necessary
   GetMem(Driver_info_2, SpaceNeeded);
   // The second GetPrinter fills in all the current settings
   GetPrinter(hPrinter, 2, Driver_info_2, SpaceNeeded, @SpaceNeeded);
   // If GetPrinter didn't fill in the DEVMODE, try to get it by calling
   // Documentproperties() - this is always true in Delphi, so ...
   // Zero in last arg returns buffer size - if it's -1 the call failed..
   DevModeSize := DocumentProperties(0, hPrinter, Printername, pDBuff, pDBuff, 0);
   // get space
   dPtr := allocmem(DevModeSize);
   devmode := pDevmode(dPtr);
   // this call fills our DevMode struct
   DocumentProperties(0, hPrinter, Printername, _devicemodeA(dPtr^), pDBuff, DM_OUT_BUFFER);

   // make changes to devmode
   devmode^.dmFields := DM_DEFAULTSOURCE;

   try
      // here is where it goes wrong. This generates a Access Violation
      SetPrinter(hPrinter, 2, devmode, 0);
      // tried to comment the SetPrinter and let this statement execute. Goes OK, but later on I get Access Violation without any reason.
      DocumentProperties(0, hPrinter, Printername, _devicemodeA(dPtr^), _devicemodeA(dPtr^), DM_IN_BUFFER + DM_OUT_BUFFER);
   except
       // bomb out. If it's a 'Printer index out of bounds' it might
       // be an invalid code.
   end;
   FreeMem(Driver_info_2, SpaceNeeded);
   Freemem(dPtr, DevmodeSize);
   Closeprinter(hPrinter);
end;

Thanks in advance,
Benjamin
0
Comment
Question by:b_vaneck
4 Comments
 
LVL 3

Expert Comment

by:MikProg
ID: 10876514
Why you leave of no use Delphi native printer object. Printer.Handle already contains handle you get throught OpenPrinter(Printername, hPrinter, nil); It excludes errors when you specify illegal printer by name (I think 'Printer index out of bounds' means that case).
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10876750

  uses
    printers;

  var
    priners : TPrinter;
  ...


  it has method :
    procedure SetPrinter(ADevice, ADriver, APort: PChar; ADeviceMode: THandle);
0
 
LVL 17

Accepted Solution

by:
mokule earned 1000 total points
ID: 10876761

You can select printer by

Printer.PrinterIndex := Printer.Printers.IndexOf(PrinterName);
0
 
LVL 3

Expert Comment

by:SuperUt
ID: 10876805
Using 'printer' it is also very easy to have a list of the available printers.
This shows how you can use a combobox to choose the printer from.

var     ComboPrinter: TComboBox;

procedure TForm1.FormCreate(Sender: TObject);
...
   // Printerlist
      ComboPrinter.Items:= Printer.Printers;
      ComboPrinter.Text := Printer.Printers[ Printer.PrinterIndex];



procedure TForm1.ComboPrinterChange(Sender: TObject);
begin
   Printer.SetPrinter( pchar( ComboPrinter.text), '','',0);
end;
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses

807 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