Solved

Setting output printer

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

15 Experts available now in Live!

Get 1:1 Help Now