Solved

Setting the desired printer (Easy)

Posted on 1999-01-22
3
151 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Communication Between RC4 Delphi <-> PHP 3 123
control image tags in a string ? 12 142
Convert MS Word document to a PDF file 9 91
DBGrid or StringGrid ? 6 105
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 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