Solved

Setting the desired printer (Easy)

Posted on 1999-01-22
3
147 Views
Last Modified: 2010-04-06
I sometimes need to change the default printer.  I am using the SetPrinter procedure but am not getting any results.  I look in the printer setup dialog and see that it is set, but it does not print there.  Help.
0
Comment
Question by:swig
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 10 total points
ID: 1362838
Hi swig,

this is the function I'm using. I hope it solves your problems...

function SetDefaultPrinter(localPrinter: string) : boolean;
var c1   : cardinal;
    pi   : PPrinterInfo2;
    size : cardinal;
begin
    result:=false;
    if OpenPrinter(PChar(localPrinter),c1,@printerDefaults) then begin
      try
        GetPrinter(c1,2,nil,0,@size); pi:=AllocMem(size);
        try
          if not GetPrinter(c1,2,pi,size,@size) then begin
            // AlertError('Standard-Drucker festlegen (1).',LastErrStr);
            exit;
          end;
          pi^.pSecurityDescriptor:=nil;
          if pi^.Attributes and PRINTER_ATTRIBUTE_DEFAULT=0 then begin
            pi^.Attributes:=pi^.Attributes or PRINTER_ATTRIBUTE_DEFAULT;
            if not SetPrinter(c1,2,pi,0) then begin
              // AlertError('Standard-Drucker festlegen (2).',LastErrStr);
              exit;
            end;
          end;
          result:=true;
        finally FreeMem(pi) end;
      finally ClosePrinter(c1) end;
    end else ;
      // AlertError('Standard-Drucker festlegen.',
      //            'Der Drucker "'+localPrinter+'" wurde nicht gefunden!');
end;

Regards, Madshi.
0
 

Author Comment

by:swig
ID: 1362839
I am unfamiliar with PPrinterInfo2.  Could you explain?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1362840
type
  TPrinterInfo2 = record
    pServerName: PAnsiChar;
    pPrinterName: PAnsiChar;
    pShareName: PAnsiChar;
    pPortName: PAnsiChar;
    pDriverName: PAnsiChar;
    pComment: PAnsiChar;
    pLocation: PAnsiChar;
    pDevMode: PDeviceModeA;
    pSepFile: PAnsiChar;
    pPrintProcessor: PAnsiChar;
    pDatatype: PAnsiChar;
    pParameters: PAnsiChar;
    pSecurityDescriptor: PSecurityDescriptor;
    Attributes: DWORD;
    Priority: DWORD;
    DefaultPriority: DWORD;
    StartTime: DWORD;
    UntilTime: DWORD;
    Status: DWORD;
    cJobs: DWORD;
    AveragePPM: DWORD;
  end;

I'm doing this:
(1) Opening the printer.
(2) Getting the size of the PPrinterInfo2, that is needed (the strings are stored behind the structure, so the allocated buffer must be bigger than just the buffer size.
(3) Get the buffer (including the strings).
(4) Now I have the current printer settings.
(5) Change the attributes, so that this printer is the default printer now.
(6) Set the buffer.
(7) Free the buffer.
(8) Close the printer.

Everything clear now?

Regards, Madshi.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

16 Experts available now in Live!

Get 1:1 Help Now