Sign in to follow this  
Followers 0
mbubola

Set Default Printer on RDS 2008 R2 or 2012 R2

3 posts in this topic

We have a RDSH Platform with mandatory profile the printers is deplyed by GPO but set defaul printer lost after logoff session.

I found this nice autoit script but it work on tray icons:

#include <Array.au3>
#include <Constants.au3>
; Turn off default tray menu options
Opt("TrayMenuMode", 1)
; Set some program constants
Const $ProgramName   = "Default Printer Selector"
Const $ProgramVersion = "1.00"
Const $WindowTitle  = $ProgramName & " " & $ProgramVersion
; Change the tray icon
TraySetIcon(@SystemDir & "SHELL32.dll", 17)
; Set the tray program name
TraySetToolTip($WindowTitle)
; Store result of _GetPrinters() function
$arrPrinters = _GetPrinters()
; Check if any printers were found
If UBound($arrPrinters) = 1  Then
   MsgBox(16, $WindowTitle, "Error - No Printers Found for user " & @Username)
Else
     
   ; Get the default printer name
   $strDefaultPrinter = _GetDefaultPrinter()
  
   ; Create the menu
   For $i = 1 to UBound($arrPrinters)-1
  
   ; Store the menu handle in the 3rd column
   $arrPrinters[$i][2] = TrayCreateItem($arrPrinters[$i][0])
  
   ; Set the tick mark if this is the current default printer
   If $arrPrinters[$i][0] = $strDefaultPrinter Then
   TrayItemSetState(-1, $TRAY_CHECKED)
   Else
   TrayItemSetState(-1, $TRAY_UNCHECKED)
   EndIf
  
   Next
   ; Create a tray menu seperator
   TrayCreateItem("")
  
   ; Create an Exit option
   $mnuExit = TrayCreateItem("Exit")
  
   While 1
   ; Get any tray menu messages
   $tMsg = TrayGetMsg()
  
   ; Quit if user clicks exit
   If $tMsg = $mnuExit Then Exit
  
   ; Check the tray message is one of our printer tray menu item handles
   If $tMsg >= $arrPrinters[1][2] AND $tMsg <= $arrPrinters[UBound($arrPrinters)-1][2] Then
  
   ; Search the array to find the index of the tray item handle that was clicked
   $intClickItem = _ArraySearch($arrPrinters, $tMsg)
  
   ; If the index is found in the array
   If $intClickItem > -1 Then
   
   ; Loop through all the menu items
   For $i = 1 to UBound($arrPrinters)-1
     
      If $i = $intClickItem Then
      ; Tick the item that was clicked and update registry
      TrayItemSetState($arrPrinters[$i][2], $TRAY_CHECKED)
      RegWrite("HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionWindows", "Device", "REG_SZ", $arrPrinters[$i][0] & "," & $arrPrinters[$i][1])
      Else
      ; Untick the menu item
      TrayItemSetState($arrPrinters[$i][2], $TRAY_UNCHECKED)
      EndIf
     
   Next
   
   EndIf
   EndIf
   
   WEnd
EndIf


Func _GetDefaultPrinter()
  
   ; Read the default printer name from the registry
   Local $strDefaultPrinter = RegRead("HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionWindows", "Device")
  
   ; Only need the part up to the first comma (rest of line is port details)
   $strDefaultPrinter = StringLeft($strDefaultPrinter, StringInStr($strDefaultPrinter, ",")-1)
  
   ; Return the printer name
   Return $strDefaultPrinter
  
EndFunc

Func _GetPrinters()
   ; Local array to hold one row of printer details - printer name, port details, tray menu handle
   Local $arrLocalPrinters[1][3]
   ; Counter for looping through registry
   Local $i = 1
   ; While there are still registry entries to read
   While 1
   ; String location of registry to read
   Local $strREGKEY = "HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionPrinterPorts"
  
   ; Get the next printer name from the registry value
   Local $strPrinter = RegEnumVal($strRegKey, $i)
  
   ; Exit if there are no more registry entries
   If @error Then ExitLoop
   ; Read the printer port details for this printer
   Local $strPort = RegRead("HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionPrinterPorts", $strPrinter)
  
   ; Only need the part from start of string to first colon
   $strPort = StringLeft($strPort, StringInStr($strPort, ":"))
   ; Add this printer to the array
   Local $intMax = UBound($arrLocalPrinters)
  
   ; Add an new row to the array
   ReDim $arrLocalPrinters[$intMax+1][3]
   ; Store the printer name in column 1
   $arrLocalPrinters[$intMax][0] = $strPrinter
  
   ; Store the printer port details in column 2
   $arrLocalPrinters[$intMax][1] = $strPort
   ; Increase the loop counter to get the next registry value
   $i += 1
   WEnd
   ; Sort the printer array alphabetically
   _ArraySort($arrLocalPrinters)
   ; Return the array
   Return $arrLocalPrinters
     
EndFunc

I ask you to help me please to trasform this script illustrated upper on GUICtrlCreateCombo mode with scroll up/down and set default printer and export the registry key on the shared folder or write a text file of printers but I'm expert able to create this program

Thanks and help me soon possible please

Mario

ComboBox.png

Share this post


Link to post
Share on other sites



Hello firiends

you can help me to develop this solution please?

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Today i found this thread because i am looking too for an solution to autoset the Default Printer via autostart / cmd-script. After about 1 hours of work here is my Kind of solution....

#include <Array.au3>
   #include <Constants.au3>

   #include <GUIConstantsEx.au3>
   #include <MsgBoxConstants.au3>

   ; Turn off tray icon
   Opt("TrayIconHide", 1)

   ; Set some program constants
   Const $ProgramName   = "Default Printer Selector"
   Const $ProgramVersion = "1.0.1"
   Const $WindowTitle  = $ProgramName & " " & $ProgramVersion
   Const $INIfile="SetDefaultPrinter.ini"

   If not FileExists($INIFile) Then
      FileWrite($INIFile, "; INI File to store the last DefaultPrinter")
      FileWrite($INIFile, @CRLF)
   EndIf

   ; Store result of _GetPrinters() function
   $arrPrinters = _GetPrinters()
   ; Check if any printers were found
   If UBound($arrPrinters) = 1  Then
      MsgBox(16, $WindowTitle, "Error - No Printers Found for user " & @Username)
      Exit
   EndIf


   ; Get the default printer name
   $strDefaultPrinter = _GetDefaultPrinter()
   ; read last selected DefaultPrinter from INI file
   $strINIDefaultPrinter=IniRead($INIfile, "Options", "DefaultPrinter", "")

   ; check for parameter "autoset"
   if $cmdline[0] > 0 Then
      if StringInStr( $cmdline[1], "autoset") then
         For $i = 1 to UBound($arrPrinters)-1
            If $arrPrinters[$i][0] = $strINIDefaultPrinter Then
               RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device", "REG_SZ", $arrPrinters[$i][0] & "," & $arrPrinters[$i][1])
               INIWrite($INIfile, "Options", "DefaultPrinter", $arrPrinters[$i][0])
            EndIf
         Next
      Else
         Msgbox(0,"Parameter Error", "The given parameter '" & $cmdline[1] & "' isn't correct." & @CRLF & @CRLF & "use the paramater AUTOSET.", 5)
      EndIf
      Exit
   EndIf


   ; create GUI and fill the ComboControl
   Global $GUI1id = GUICreate("Set Default Printer", 600, 80)
   Global $GUI1Select=GUICtrlCreateCombo("", 10, 12, 440, 21)
   GUICtrlSetFont(-1, 14)
   Global $GUI1Button1=GUICtrlCreateButton("Save and Exit", 460, 10, 130, 32)
   GUICtrlSetFont(-1, 14)
   $temp=$arrPrinters[1][0]
   For $i = 2 to UBound($arrPrinters)-1
      $temp=$temp & "|" & $arrPrinters[$i][0]
   Next
   GUICtrlSetData($GUI1Select, $temp, $strDefaultPrinter)
   GUISetState()



   While 1
      ; Get any tray menu messages
      $guiMsg = GUIGetMsg()
      $tMsg = TrayGetMsg()
      ; Quit if user clicks exit
      If $guiMsg=$GUI_EVENT_CLOSE Then
         INIWrite($INIfile, "Options", "DefaultPrinter", $strDefaultPrinter)
         Exit
      EndIf
      If $guiMsg=$GUI1Button1 Then
         For $i = 1 to UBound($arrPrinters)-1
            If GUICtrlRead($GUI1Select) = $arrPrinters[$i][0] Then
               RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device", "REG_SZ", $arrPrinters[$i][0] & "," & $arrPrinters[$i][1])
               INIWrite($INIfile, "Options", "DefaultPrinter", $arrPrinters[$i][0])
            EndIf
         Next
         Exit
      EndIf
   WEnd


Func _GetDefaultPrinter()

   ; Read the default printer name from the registry
   Local $strDefaultPrinter = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device")

   ;msgbox(0,"", $strDefaultPrinter)
   ; Only need the part up to the first comma (rest of line is port details)
   $strDefaultPrinter = StringLeft($strDefaultPrinter, StringInStr($strDefaultPrinter, ",")-1)

   ; Return the printer name
   Return $strDefaultPrinter

EndFunc

Func _GetPrinters()
   ; Local array to hold one row of printer details - printer name, port details, tray menu handle
   Local $arrLocalPrinters[1][3]
   ; Counter for looping through registry
   Local $i = 1
   ; While there are still registry entries to read
   While 1
   ; String location of registry to read
   Local $strREGKEY = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"

   ; Get the next printer name from the registry value
   Local $strPrinter = RegEnumVal($strRegKey, $i)

   ; Exit if there are no more registry entries
   If @error Then ExitLoop
   ; Read the printer port details for this printer
   Local $strPort = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts", $strPrinter)

   ; Only need the part from start of string to first colon
   $strPort = StringLeft($strPort, StringInStr($strPort, ":"))
   ; Add this printer to the array
   Local $intMax = UBound($arrLocalPrinters)

   ; Add an new row to the array
   ReDim $arrLocalPrinters[$intMax+1][3]
   ; Store the printer name in column 1
   $arrLocalPrinters[$intMax][0] = $strPrinter

   ; Store the printer port details in column 2
   $arrLocalPrinters[$intMax][1] = $strPort
   ; Increase the loop counter to get the next registry value
   $i += 1
   WEnd
   ; Sort the printer array alphabetically
   _ArraySort($arrLocalPrinters)
   ; Return the array
   Return $arrLocalPrinters

EndFunc

 

starting the script will present the Combobox with all Printers..

 

Screenshot.jpg

Now you can select the Printer

With an click at "save and Exit" the Default printer will set and stored into the ini-file "SetDefaultPrinter.ini". The INI File is stored und searched at Script Path.

with this ini file it's possible to set the Default Printer automatically by starting the script with an parameter "/autoset".

 

SetDefaultPrinter.au3

Edited by tresjam

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0