From: "Stephen Brown" Subject: Re: (D5) Select a printer ? Date: 13 Oct 1999 00:00:00 GMT Message-ID: <7u2vjo$ssv$6@gxsn.com> References: <7u1fkc$pef$1@minus.oleane.net> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Complaints-To: abuse@gxsn.com X-Trace: 939852216 1NNUCNF1GAC4EC393C gxsn.com Organization: GXSN X-MSMail-Priority: Normal Reply-To: "Stephen Brown" Newsgroups: comp.lang.pascal.delphi.misc Michel Peyron wrote in message news:7u1fkc$pef$1@minus.oleane.net... > How can I make a printer selected in a TPrinterSetupDialog the > default printer for my application ? The following code sets the currently selected printer as the persistent default. Note the calls to the two different GetPrinter functions - one in the Printers unit and the other in Winspool. uses Printers, Winspool; procedure ChangePrinter; var ADevice, ADriver, APort: array[0..100] of Char; HDeviceMode, HPrinter: THandle; PrinterInfo2: PPrinterInfo2; Size: DWORD; begin ZeroMemory(@ADevice, SizeOf(ADevice)); // Get the device name of the current printer Printer.GetPrinter(ADevice, ADriver, APort, HDeviceMode); // Initialise some memory for the TPrinterInfo2 structure Size := 0; GetMem(PrinterInfo2, Size); try // Get a handle to the current printer if not OpenPrinter(ADevice, HPrinter, nil) then begin ShowMessage('Error getting handle to current printer.' + #13 + SysErrorMessage(GetLastError)); Exit; end; // Get size of printer info structure Winspool.GetPrinter(HPrinter, 2, PrinterInfo2, Size, @Size); // Allocate memory for printer info structure ReAllocMem(PrinterInfo2, Size); // Get printer info for current printer if not Winspool.GetPrinter(HPrinter, 2, PrinterInfo2, Size, @Size) then begin ShowMessage('Error getting current printer info.' + #13 + SysErrorMessage(GetLastError)); Exit; end; // Modify the printer info to include the default attribute with PrinterInfo2^ do Attributes := Attributes + PRINTER_ATTRIBUTE_DEFAULT; // Apply the modified printer info to the current printer if not Winspool.SetPrinter(HPrinter, 2, PrinterInfo2 , 0) then begin ShowMessage('Error setting default printer.' + #13 + SysErrorMessage(GetLastError)); Exit; end; finally FreeMem(PrinterInfo2); if HPrinter <> 0 then ClosePrinter(HPrinter); end end; -- Stephen Brown