Solved

Setting the desired printer (Easy)

Posted on 1999-01-22
3
153 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
[X]
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
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

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