Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Get Default Printer Name


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 3 - Last Post: Feb 15, 2018 11:13 AM Last Post By: Jeff Overcash (...
Michael Eriksen

Posts: 27
Registered: 6/12/11
Get Default Printer Name  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 15, 2018 8:57 AM
Hello all,

How can I get the default printername without setting printer.printerindex := -1 ??
Does somebody have a function for returning the name of the default printer?

I'm using Delphi XE8.

Thanks in advance,
Regards Michael Eriksen
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Get Default Printer Name  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 15, 2018 9:40 AM   in response to: Michael Eriksen in response to: Michael Eriksen
On 2/15/2018 11:57 AM, Michael Eriksen wrote:
Hello all,

How can I get the default printername without setting printer.printerindex := -1 ??
Does somebody have a function for returning the name of the default printer?

I'm using Delphi XE8.

Thanks in advance,
Regards Michael Eriksen

Try this. It is pulling pieces from TPrinters GetDefaultPrinter to only get the
name

uses
   WinAPI.WinSpool;
 
{$IF DEFINED(UNICODE) AND DEFINED(MSWINDOWS)}
function GetDefaultPrinter(DefaultPrinter: PChar; var I: Integer): BOOL; stdcall;
   external winspl name 'GetDefaultPrinterW';
{$ENDIF}
 
function GetDefaultPrinterName : string;
var
   DefaultPrinter: array[0..1023] of Char;
   i : Integer;
begin
{$IF DEFINED(UNICODE)}
   I := Length(DefaultPrinter);
   if not GetDefaultPrinter(DefaultPrinter, I) then
     ZeroMemory(@DefaultPrinter[0], I * SizeOf(Char));
{$ELSE}
   GetProfileString('windows', 'device', '', DefaultPrinter, 
SizeOf(DefaultPrinter) - 1);
{$ENDIF}
   Result := DefaultPrinter;
end;


--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Get Default Printer Name  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 15, 2018 10:16 AM   in response to: Jeff Overcash (... in response to: Jeff Overcash (...
Jeff Overcash (TeamB) wrote:

{$IF DEFINED(UNICODE) AND DEFINED(MSWINDOWS)}
function GetDefaultPrinter(DefaultPrinter: PChar; var I: Integer):
BOOL; stdcall; external winspl name 'GetDefaultPrinterW';
{$ENDIF}

I wonder why TPrinter resorted to using GetProfileString() in ANSI
builds and not use GetDefaultPrinterA() instead?

--
Remy Lebeau (TeamB)
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Get Default Printer Name  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 15, 2018 11:13 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
On 2/15/2018 1:16 PM, Remy Lebeau (TeamB) wrote:
Jeff Overcash (TeamB) wrote:

{$IF DEFINED(UNICODE) AND DEFINED(MSWINDOWS)}
function GetDefaultPrinter(DefaultPrinter: PChar; var I: Integer):
BOOL; stdcall; external winspl name 'GetDefaultPrinterW';
{$ENDIF}

I wonder why TPrinter resorted to using GetProfileString() in ANSI
builds and not use GetDefaultPrinterA() instead?

No idea, but that way you can find in google back in 2002 so I just assume old
legacy code that no one ever looked at to update. The signature for
GetDefaultPrinter (the Integer signature isn't a Var, but an int pointer IIRC
from looking at it earlier) is slightly different so instead of playing with it
to use the WinSpool definition I just did exactly what was going on in Printers.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02