Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to set the port for a specific printer Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
07-Oct-02
Category
Reporting /Printing
Language
Delphi 2.x
Views
70
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I want to change the default printer and its settings (port) under Win 9x so that 
it affects all other applications for automated document printing to files (not 
from my application, from others like CorelDraw and Word...). I tried to this by 
changing the registry entries for the printers, but these changes only take effect 
after rebooting the system. Is there an API function that causes windows to update 
the printer settings from the registry? Or any other API function that directly 
affects the system wide printer settings?

Answer:

Setting a port for a specific printer:

1   uses
2     WinSpool;
3   
4   { Function SetPrinterToPort
5     Parameters :
6       hPrinter: handle of printer to change, obtained from OpenPrinter
7       port: port name to use, e.g. LPT1:, COM1:, FILE:
8     Returns:
9       The name of the previous port the printer was attached to.
10    Description:
11      Changes the port a printer is attached to using Win32 API functions.
12  		The changes made are NOT local to this process, they will affect all 
13  		other processes that try to use this printer! It is recommended to set the 
14  		port back to the old port returned by this function after 
15  		the end of the print job.
16    Error Conditions:
17     Will raise EWin32Error exceptions if SetPrinter or GetPrinter fail.
18    Created:
19      21.10.99 by P. Below}
20  
21  function SetPrinterToPort(hPrinter: THandle; const port: string): string;
22  var
23    pInfo: PPrinterInfo2;
24    bytesNeeded: DWORD;
25  begin
26    {Figure out how much memory we need for the data buffer. Note that GetPrinter is
27    supposed to fail with a specific error code here. The amount of memory will 
28  	be larger than Sizeof(TPrinterInfo2) since variable amounts of data are appended 
29  	to the record}
30    SetLastError(NO_ERROR);
31    GetPrinter(hPrinter, 2, nil, 0, @bytesNeeded);
32    if GetLastError <> ERROR_INSUFFICIENT_BUFFER then
33      RaiseLastWin32Error;
34    pInfo := AllocMem(bytesNeeded);
35    try
36      if not GetPrinter(hPrinter, 2, pInfo, bytesNeeded, @bytesNeeded) then
37        RaiseLastWin32Error;
38      with pInfo^ do
39      begin
40        Result := pPortname;
41        pPortname := @port[1];
42      end;
43      if not SetPrinter(hPrinter, 2, pInfo, 0) then
44        RaiseLastWin32Error;
45    finally
46      FreeMem(pInfo);
47    end;
48  end;
49  
50  function GetCurrentPrinterHandle: THandle;
51  var
52    Device, Driver, Port: array[0..255] of char;
53    hDeviceMode: THandle;
54  begin
55    Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
56    if not OpenPrinter(@Device, Result, nil) then
57      RaiseLastWin32Error;
58  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC