AutoIt Forums: Default Printer location - AutoIt Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Default Printer location

#1 User is offline   tazdev 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 86
  • Joined: 04-January 05

Posted 16 May 2005 - 08:03 PM

Anyone know where the setting is located in the registry for the default printer?
I am working on a routine that I will post that will change the network printers installed on a Windows NT + machine from one server to another. I am doing this as part a migration. I just want to be able to set the user's default printer to what it should be since the server change actually requires me to create a new key. With that change the computer loses it's default printer. I would like to set it to the replaced printer. Any ideas??
I was thinking of just changing the registry setting but I can't find where it is. So far I will have to send a popup to the user that they need to change it. Press button one to open printers to change it or button 2 to do it later.

#2 User is offline   tazdev 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 86
  • Joined: 04-January 05

Posted 16 May 2005 - 11:26 PM

Yeah I couldn't find it either. Sucks.

#3 User is offline   DaveF 

  • Smells fanboyish.
  • Icon
  • Group: Developers(Dev)
  • Posts: 637
  • Joined: 28-May 04
  • Location:Arkansas, USA

Posted 17 May 2005 - 12:18 AM

Not sure about NT, on Win XP printer prefs are per-user, so the default printer name can be found at:
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows:Device
...the data is a comma-separated entry of the name, driver and port name.

If you really want a power tool for doing this chore you can get PRNADMIN.DLL from the Windows 2003 Resource Kit. The included PRNADMIN.DOC file gives examples in VBScript, you could either make and FileInstall VB scripts and Run them with cscript.exe from AutoIt, or if you were feeling really empowered you could use the AutoIt3 beta and create and handle the PrintMaster COM objects from AutoIt itself...

#4 User is offline   /dev/null 

  • Mass Spammer!
  • Icon
  • Group: AutoIt MVPs(MVP)
  • Posts: 2,946
  • Joined: 15-May 05

Posted 17 May 2005 - 02:55 AM

tazdev, on May 16 2005, 11:03 AM, said:

Anyone know where the setting is located in the registry for the default printer?


Try this...

RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "printer"

More info here ...

http://www.robvanderwoude.com/2kprintcontrol.html

I'm not 100% sure, but monitoring the command above with regmon,
where "printer" was replaced with one of my printers, gives me:

HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device="Brother MFC-9880 USB,winspool,Ne07:"


Cheers
Kurt

#5 User is offline   CyberSlug 

  • Overwhelmed with work....
  • Icon
  • Group: AutoIt MVPs(MVP)
  • Posts: 3,587
  • Joined: 03-December 03
  • Location:Kentucky, USA

Posted 17 May 2005 - 03:15 AM

I'm feeling generous. I've used the following on 98/2000/XP machines at work:
[ code='text' ]    ( ExpandCollapse - Popup )
; Default printer is denoted with astrisk; shared printers are denoted with $ then share name MsgBox(4096, "Printer Info", _Identify_Printers() ) ; CyberSlug - 18 Nov 2004 ; List installed printers (port name drive and shareNameIfApplicable)  also indentify default printer Func _Identify_Printers()    Local $key, $default, $i = 1, $list, $name    If @OSType = "WIN32_WINDOWS" Then       $key = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Print\Printers"       $default = RegRead("HKEY_CURRENT_CONFIG\System\CurrentControlSet\Control\Print\Printers","Default")    Else;WIN_NT type       $key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers"       $default = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device")       $default = StringLeft($default, StringInStr($default,",")-1);remove info after comma...    EndIf   ;;;$list = StringReplace($list, $default, '** ' & $default)   ; ENUMERATE LOCAL PRINTERS (AND ALSO 9x PRINTERS SHARED ON OTHER COMPUTERS)    While 1      ;#cs - enum bug workaround          RegRead($key, "")          If @error = 1 Then ExitLoop      ;#ce       Local $name = RegEnumKey($key, $i)       If @error Then ExitLoop       Local $port = "<" & RegRead($key & "\" & $name, "Port") & ">"       If $name = $default Then $port = '* ' & $port       Local $driver = RegRead($key & "\" & $name, "Printer Driver")       Local $shared = RegRead($key & "\" & $name, "Share Name")       If $shared <> "" Then $shared = "$ " & $shared       $list = $list & $port & "  " & $name & "  [" & $driver & "]  " & $shared & @CRLF       $i = $i + 1    WEnd   ; ENUMERATE 2000/XP PRINTERS SHARED ON OTHER COMPUTERS    If @OSType = "WIN_NT" Then       Local $i = 1, $j = 1       While 1         ;#cs - enum bug workaround             RegRead($key, "")             If @error = 1 Then ExitLoop         ;#ce          Local $serverAddress = RegEnumKey($key, $i)          If @error Then ExitLoop          $j = 1          While 1            ;#cs - enum bug workaround                RegRead($key & "\" & $serverAddress & "\Printers", "")                If @error = 1 Then ExitLoop(2)            ;#ce             Local $networkedPrinter = RegEnumKey($key & "\" & $serverAddress & "\Printers", $j)             If @error Then ExitLoop(2)             $name = RegRead($key & "\" & $serverAddress & "\Printers\" & $networkedPrinter, "Name")             Local $share = "<\\" & $serverAddress & "\" & RegRead($key & "\" & $serverAddress & "\Printers\" & $networkedPrinter, "Share Name") & ">"             If $name = $default Then $share = '* ' & $share             $list = $list & $share & "  " & $name & "  [" & $driver & "]" & @CRLF             $j = $j + 1          WEnd          $i = $i + 1       WEnd    EndIf    If $list = "" Then $list = "No printers detected" & @CRLF    Return $list EndFunc


#6 User is offline   tazdev 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 86
  • Joined: 04-January 05

Posted 17 May 2005 - 05:19 AM

/dev/null, on May 16 2005, 06:55 PM, said:

HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device="Brother MFC-9880 USB,winspool,Ne07:"
Cheers
Kurt


Yep that is the key. I wil lcheck out the site. I need to see what nexx will be for the new printer and then I can just change it myself in the script. As it stands now I can give them what their default is and do it that way with winwaitactive and send.

#7 User is offline   hum3 

  • Newbie
  • Group: Members
  • Posts: 3
  • Joined: 14-September 07

Posted 14 September 2007 - 11:57 AM

I just updated this as I wanted to use it and didn't quite work on my XP machine
CODE
; Default printer is denoted with astrisk; shared printers are denoted with $ then share name
MsgBox(4096, "Printer Info |" & @OSTYPE & "|", _Identify_Printers())

; CyberSlug - 18 Nov 2004
; Hum3 - 2007-09-14
; List installed printers (port name drive and shareNameIfApplicable) also indentify default printer
Func _Identify_Printers()
Local $key, $default, $i = 1, $list, $name
If @OSTYPE = "WIN32_WINDOWS" Then
$key = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Print\Printers"
$default = RegRead("HKEY_CURRENT_CONFIG\System\CurrentControlSet\Control\Print\Printers", "Default")
ElseIf @OSTYPE = "WIN_NT" Then;WIN_NT type
$key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers"
$netKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers"
$default = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device")
$default = StringLeft($default, StringInStr($default, ",") - 1);remove info after comma...
Else ;assume "WIN32_NT"
$key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers"
$netKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\LanMan Print Services\Servers"
$default = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device")
$default = StringLeft($default, StringInStr($default, ",") - 1);remove info after comma...
EndIf

;;;$list = StringReplace($list, $default, '** ' & $default)

; ENUMERATE LOCAL PRINTERS (AND ALSO 9x PRINTERS SHARED ON OTHER COMPUTERS)
While 1
;#cs - enum bug workaround
RegRead($key, "")
If @error = 1 Then ExitLoop
;#ce
Local $name = RegEnumKey($key, $i)
If @error Then ExitLoop
Local $port = "<" & RegRead($key & "\" & $name, "Port") & ">"
If $name = $default Then $port = '* ' & $port
Local $driver = RegRead($key & "\" & $name, "Printer Driver")
Local $shared = RegRead($key & "\" & $name, "Share Name")
If $shared <> "" Then $shared = "$ " & $shared
$list = $list & $port & " " & $name & " [" & $driver & "] " & $shared & @CRLF
$i = $i + 1
WEnd

; ENUMERATE 2000/XP PRINTERS SHARED ON OTHER COMPUTERS
If @OSTYPE <> "WIN32_WINDOWS" Then
Local $i = 1, $j = 1
While 1
;#cs - enum bug workaround
RegRead($netKey, "")
If @error = 1 Then ExitLoop
;#ce
Local $serverAddress = RegEnumKey($netKey, $i)
If @error Then ExitLoop
$j = 1
While 1
;#cs - enum bug workaround
RegRead($netKey & "\" & $serverAddress & "\Printers", "")
If @error = 1 Then ExitLoop (2)
;#ce
Local $networkedPrinter = RegEnumKey($netKey & "\" & $serverAddress & "\Printers", $j)
If @error Then ExitLoop (2)
$name = RegRead($netKey & "\" & $serverAddress & "\Printers\" & $networkedPrinter, "Name")
Local $share = "<\\" & $serverAddress & "\" & RegRead($netKey & "\" & $serverAddress & "\Printers\" & $networkedPrinter, "Share Name") & ">"
If $name = $default Then $share = '* ' & $share
$list = $list & $share & " " & $name & " [" & $driver & "]" & @CRLF
$j = $j + 1
WEnd
$i = $i + 1
WEnd
EndIf

If $list = "" Then $list = "No printers detected" & @CRLF
Return $list
EndFunc ;==>_Identify_Printers


#8 User is offline   hum3 

  • Newbie
  • Group: Members
  • Posts: 3
  • Joined: 14-September 07

Posted 14 September 2007 - 05:34 PM

An Update.

What I really wanted was a list of printer names that matched the list in ActivePrinter in Excel. This is actually slightly different from the code above and this is what I have got to incase anyone else wants it. i have only tested in Excel 2003

CODE
;Returns a list which should match ActivePrinter and ing the same order
Func _PrinterList()
Local $key, $list, $port, $printer, $printerList[99], $numPrinters ;Set to only work for max of 99 printers
$numPrinters = 0
$key = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices"
$j = 1
While 1
$printer = RegEnumVal($key, $j)
If @error < 0 Then ExitLoop
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $printer=RegEnumKey($key,$j) = ' & "Prob 1? " & $j & " " & $printer & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$port = RegRead($key ,$printer)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : RegRead($key,$printer) = ' & "Prob? " & $port & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$port = StringRight($port,StringLen($port)-StringInStr($port,","))
$printerList[$numPrinters] = $printer & " on " & $port
$numPrinters += 1
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $numPrinters = ' & $numPrinters & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$j = $j + 1
WEnd
if $numPrinters > 0 Then
ReDim $printerList[$numPrinters]
Else ;no printers set up
ReDim $printerList[1]
$printerList[0] = "No printers setup"
EndIf
Return $printerList
EndFunc ;==>_PrinterList


#9 User is offline   ptrex 

  • Mass Spammer!
  • Icon
  • Group: AutoIt MVPs(MVP)
  • Posts: 2,221
  • Joined: 03-June 05

Posted 14 September 2007 - 07:59 PM

@all

Less lines of code needed with the WMI approach.

[ autoIt ]    ( Popup )
Const $DEFAULT = 4 $strComputer = "." $objWMIService = ObjGet ("winmgmts:\\" & $strComputer & "\root\cimv2") $colPrinters = $objWMIService.ExecQuery ("Select * From Win32_Printer")       For $objPrinter in $colPrinters     If $objPrinter.Attributes And $DEFAULT Then         If $objPrinter.ShareName <> "" Then         ConsoleWrite( $objPrinter.ShareName & @CRLF )         EndIf     EndIf Next


Regards,

ptrex

#10 User is offline   ptrex 

  • Mass Spammer!
  • Icon
  • Group: AutoIt MVPs(MVP)
  • Posts: 2,221
  • Joined: 03-June 05

Posted 14 September 2007 - 08:11 PM

@all

Wrong code, sorry guys.

This is the correct one and even less lines of code ;)

[ autoIt ]    ( Popup )
$strComputer = "." $objWMIService = ObjGet ("winmgmts:\\" & $strComputer & "\root\cimv2") $colPrinters = $objWMIService.ExecQuery ("Select * From Win32_Printer Where Default = True")       For $objPrinter in $colPrinters         ConsoleWrite($objPrinter.Name & @CRLF) Next


regards,

ptrex

#11 User is offline   weaponx 

  • I'm coming for blood, no code of conduct, no law.
  • Icon
  • Group: AutoIt MVPs(MVP)
  • Posts: 5,298
  • Joined: 26-August 05
  • Gender:Male
  • Location:Fremont, OH

Posted 14 September 2007 - 08:16 PM

This thread was dead and buried, now it is walking among us in a lifeless state feasting on the flesh of the living. GREAT

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users