Setting the desired printer (Easy)

Posted on 1999-01-22
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.
Question by:swig
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 20

Accepted Solution

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;
    if OpenPrinter(PChar(localPrinter),c1,@printerDefaults) then begin
        GetPrinter(c1,2,nil,0,@size); pi:=AllocMem(size);
          if not GetPrinter(c1,2,pi,size,@size) then begin
            // AlertError('Standard-Drucker festlegen (1).',LastErrStr);
          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);
        finally FreeMem(pi) end;
      finally ClosePrinter(c1) end;
    end else ;
      // AlertError('Standard-Drucker festlegen.',
      //            'Der Drucker "'+localPrinter+'" wurde nicht gefunden!');

Regards, Madshi.

Author Comment

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

Expert Comment

ID: 1362840
  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;

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.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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