Change The Printer In Registry


i have a nt printer server with name VIS1 and i'm change to nt server VIS2, and my users have printer connect to VIS1

e.g \\VIS1\PRINTERNAME how can i change the settings to
\\VIS2\SAMEPRINTERNAME?

Is this registry change or not?

Karen
karen021897Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
MadshiConnect With a Mentor Commented:
karen, I've looked into MSDN and found this one:


"HOWTO: Get and Set the Default Printer in Windows
Last reviewed: March 4, 1998
Article ID: Q135387
 


--------------------------------------------------------------------------------

The information in this article applies to:
Microsoft Windows Software Development Kit (SDK) for Windows version 3.1
Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT versions 3.5, 3.51 - Microsoft Windows 95


SUMMARY
In all versions of Windows, the appropriate way to get the default printer is to use GetProfileString, and the appropriate way to set the default printer is to use WriteProfileString. This works whether the default printer information is stored in the WIN.INI file or in the registry.



MORE INFORMATION


Notes to Keep in Mind
The Device value you get or set actually contains three elements separated by commas as follows:


      <printer name>,<driver name>,<port>

For example:



      My Printer,HPPCL5MS,lpt1:

When setting the default printer, you must specify valid names for these elements. That is, you must specify a valid printer, driver, and port. If not, programs such as Print Manager may set the printer back to the previous valid printer, and other programs may become very confused. You can use the EnumPrinters API to retrieve the printer name, driver name, and port name of all available printers.
Windows 95 and Windows NT map most .INI file references to the registry. Because of this mapping, GetProfileString and WriteProfileString still function as they do under 16-bit Windows (Microsoft Windows and Windows for Workgroups).
After setting the default printer, notify all other applications of the change by broadcasting the WM_WININICHANGE message.

Sample Code

   // This code uses a sample profile string of "My Printer,HPPCL5MS,lpt1:"
   // To get the default printer for Windows 3.1, Windows 3.11,
   // Windows 95, and Windows NT use:
   GetProfileString("windows", "device", ",,,", buffer, sizeof(buffer));

   -----

   // To set the default printer for Windows 3.1 and Windows 3.11 use:
   WriteProfileString("windows", "device", "My Printer,HPPCL5MS,lpt1:");
   SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, 0L);

   -----

   // To set the default printer for Windows 95 use:
   WriteProfileString("windows", "device", "My Printer,HPPCL5MS,lpt1:");
   SendMessageTimeout(HWND_BROADCAST, WM_WININICHANGE, 0L,
   (LPARAM)(LPCTSTR)"windows", SMTO_NORMAL, 1000, NULL);

   -----

   // To set the default printer for Windows NT use:
   /* Note printer driver is usually WINSPOOL under Windows NT */
   WriteProfileString("windows", "device", "My Printer,WINSPOOL,lpt1:");
  SendMessageTimeout(HWND_BROADCAST, WM_WININICHANGE, 0L, 0L,
  SMTO_NORMAL, 1000, NULL);


There are two circumstances where the code won't work:

If the customer leaves out the SendMessage, no other application will recognize the change in the .INI settings.

If a different 32-bit application does not handle the WIN.INI change message, then it will appear to that application as if the default printer has not been changed. The user will need to exit and re-enter Windows 95 to have the other application recognize the printer change.

This is the preferred method of changing the printer if the code is to be platform independent; this method will work on Windows 3.1, Windows 95 and Windows NT."
0
 
MadshiCommented:
You can change that in the registry, but then it gets valid with the next reboot. Better is to use OpenPrinter, GetPrinter(level2), SetPrinter(level2), ClosePrinter. These are pure win32 APIs. Perhaps you can do something with the Printers Delphi object, too.

Regards, Madshi.
0
 
karen021897Author Commented:
i just have to change the server name e.g \\SERVER\PRINTER

to \\NEWSERVERNAME\SAMEPRINTER

do you have any example for these API functions?
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
sburckCommented:
If you use TPRINTER, then there is a property called PRINTER which is a TStringList of all the installed printers.  Selecting the PrinterIndex property will select the currently used printer - you can look at the strings in the stringlist to select which printer to work with.  This is Delphi's encapsulation of the SetPrinter and Getprinter.
0
 
karen021897Author Commented:
I need some example using these function "much points"

1. GetPrinter
2. SetPrinter

what i wanna do!!!

i have server with name "VIS1" and im taking all printers off that server and move to server name "VIS2" and the printers
on the end user people are still geting "VIS1" and i like to let them run login.bat - script or Delphi APP - EXE to change this value \\VIS1\PRINTERNAME to \\VIS2\PRINTERNAME
0
 
MadshiCommented:
Well, I can't write you something for that specific problem you have (no time), but I can give you my "SetDefaultPrinter" function, which calls GetPrinter and SetPrinter, too. You should see from these sources how the APIs work.
That's all I can offer you...

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.

P.S: You can replace the AlertError calls with ShowMessage.
0
 
karen021897Author Commented:
I get error on  

pi   : PPrinterInfo2;

can't find PPrinterInfo2;
0
 
karen021897Author Commented:
And
if OpenPrinter(PChar(localPrinter),c1,@printerDefaults) then begin
                                                     
0
 
MadshiCommented:
Sorry...

const printerDefaults : TPrinterdefaults = (pDatatype: nil; pDevMode: nil;
                                            DesiredAccess: PRINTER_ALL_ACCESS);

And add the unit "printers" to your uses clause. Or was it "spooler"? Or "WinSpool"? Don't remember...
0
 
karen021897Author Commented:
can i change the printer server name with these class
"TPrinter"?
0
 
sburckCommented:
Any available printer, if it's name is in the array it is accesible.
0
 
karen021897Author Commented:
How?
0
 
karen021897Author Commented:
How? do you have code?
0
 
karen021897Author Commented:
Hello!!!
0
 
sburckCommented:
Sorry, been away for a while...

The following little program is a form with a ListBox & a Button, pressing the button fills the Listbox from Printer.Printers.

Beyond that, selecting the value of Printer.PrinterIndex will select one of the printers.  Recall that this is a zero-based array, and that a selection of -1 will give you the default printer.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Printers,
  StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
     ListBox1.Items.Assign(Printer.Printers);
end;

end.
0
 
MadshiCommented:
sburck, karen wants to CHANGE the system settings about which is the default printer. The question is NOT how to ASK the default printer...

Regards, Madshi.
0
 
sburckCommented:
I misunderstood the question, I thought she just wished to direct output to a non-default printer.
0
All Courses

From novice to tech pro — start learning today.