Solved

Setting output printer

Posted on 2004-04-21
4
1,144 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 250 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

20 Experts available now in Live!

Get 1:1 Help Now