?
Solved

Setting the desired printer (Easy)

Posted on 1999-01-22
3
Medium Priority
?
154 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 20 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

770 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