Solved

Get Default Printer

Posted on 2006-11-17
3
1,691 Views
Last Modified: 2012-06-27
Hi Experts,

Can anybody show me code line(s) how to get the default printer in delphi 6?
Say provided a form and a button, when the user clicks on it, it will
display the default printer (on the machine where this program is running)
together with the default paper size and paper length.
Note that from time to time, the default printer may not be the one locally attached on this computer. It might be from another machine/computer.

TIA,
yorge
0
Comment
Question by:yorge
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
atul_parmar earned 75 total points
ID: 17963925
Very easy,

uses
  printers;

....

begin
  ShowMessage(Printer.Printers[Printer.PrinterIndex]);
end;

to know the paper size use Printer.Paperwidth and Paperheight properties.
0
 
LVL 10

Assisted Solution

by:atul_parmar
atul_parmar earned 75 total points
ID: 17963968
more precisely,
Printer.PrinterIndex := -1; // sets the printer to default; -1 for default printer
then
ShowMessage(Printer.Printers[Printer.PrinterIndex]);
...



0
 
LVL 1

Expert Comment

by:PaulShearing
ID: 23872210
Thanks atul. I have found that without first setting the default printer a reference to Printer.PrinterIndex can cause a runtime crash deep in ntdll.dll. Without setting the PrinterIndex to -1 my code would fail on the line:

If (Printer.PrinterIndex > 0) Then

My Delphi code to get the default printer name now looks like this:

Function getDefaultPrinterName(): String;
Begin
   Printer.PrinterIndex := -1;     // set to default
   If (Printer.PrinterIndex > 0) Then
      Try
         Result := Printer.Printers[Printer.PrinterIndex]
      Except
         Result := 'Unknown'
      End
   else
      Result := 'Unknown'
End;


0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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