?
Solved

Get Default Printer

Posted on 2006-11-17
3
Medium Priority
?
2,010 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 300 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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

850 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