Solved

Setting output printer

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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