Solved

Setting output printer

Posted on 2004-04-21
4
1,150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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