Page 1 of 1 1
Topic Options
#196466 - 2009-10-23 07:21 PM Setting Default Printer
DemoDog Offline
Fresh Scripter

Registered: 2009-08-20
Posts: 9
Loc: Washington DC
Unable to set default printer to $NewSrv. Do I need to escape the backslash in the statement
if SetDefaultPrinter("$NewSrv""$Ptr")=0
? "$NewSrv""$Ptr"

thanks in advance

 Code:

$NewSrv=\\vhost\
$OldSrv=\\pserver1\
;? $NewSrv
;? $OldSrv
$aDCPtrs =  "6thFloorCanon","6thFloorFieldCrew1","EngineeringPlotter_HPGL2",
"EngineeringPlotterPS","FIN_P1505N","HP4_2100","HP4_ACR","HP4_Creative",
"HP4_Engineer","HP4_Exec","HP4_Field","HP4_FINANCE","HP4_Online","HP4_OPS",
"HP4_Ops2","HP4_Prog1","HP4_Prog2","HP4_Prog3","HP4_Radio","HP4_TapeOps",
"HP4_VTL","HP5 Graphics","HP5_Aquarius","HP5_HR","HP5_Ops","HP5WJColor",
"HP_LJ1505","Phaser 8400N PS","RADIO_LJ1022n"

$defaultprn = GetDefaultPrinter()
? "$defaultprn"
For Each $Ptr in $aDCPtrs
    ? "$OldSrv""$Ptr"
   if "$defaultprn"="$OldSrv""$Ptr"
    ? "$OldSrv""$Ptr"
    ? "$NewSrv""$Ptr"
     if SetDefaultPrinter("$NewSrv""$Ptr")=0
       ? "$NewSrv""$Ptr"
     endif
   endif
;$defaultprn = GetDefaultPrinter()
;? "$defaultprn"

GoTo END

Function GetDefaultPrinter()
	$GetDefaultPrinter = Join(Split(ReadValue("HKEY_USERS\" + @sid + "\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device"), ',', 1), '')
EndFunction

:END


Edited by Allen (2009-10-23 07:28 PM)
Edit Reason: added code tags

Top
#196467 - 2009-10-23 07:48 PM Re: Setting Default Printer [Re: DemoDog]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Some of your concepts are backwards - change them and see if that helps.

Variable assignment of strings needs to be quoted -
$X = '\\server\'
not
$X=\\server\

and variables should NOT be inside quotes
$NewSrv$Ptr ?
not
? "$NewSrv""$Ptr"

Minor - the "?" is a shortcut for a CRLF sequence and should generally come at the end of output, unlike BASIC, where it is a shortcut for the PRINT command and comes first. In Kix, it works either way, but you'll see a problem when you create output that's used elsewhere, often with a leading blank line and no closing CRLF.

Also, the GOTO END serves no purpose other than to use a "forbidden command" (GOTO's are BAD!). Function declarations don't need to be "skipped over" the way you have coded.
Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#196471 - 2009-10-26 10:14 AM Re: Setting Default Printer [Re: Glenn Barnas]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
1. Make sure that the printer name and the share name are identical on the print server.

2. Check the error code for the reason that the set default failed:
 Code:
If SetDefaultPrinter($NewSrv+$Ptr)
   "Could not set printer "+$Ptr+" on server "+$NewSrv+" as default"+@CRLF
   "Reason: ["+@ERROR+"] "+@SERROR+@CRLF
Else
   "Printer "+$Ptr+" on server "+$NewSrv+" set as default"+@CRLF
endif

Top
#196472 - 2009-10-26 10:48 AM Re: Setting Default Printer [Re: Richard H.]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Here is a full example which also maps the printer if it does not already exists on the client.

Not fully tested.

 Code:
Break ON
$=SetOption("Explicit","ON")

Dim $sOldSrv,$sNewSrv,$sOldSrc,$asPrt,$sPrt,$sDefPath,$sDefPrt,$sDefSrv
Dim $iVerbose, $iMapped

$iVerbose=1	; 1=Display debug messages, 0=Don't display

$sOldSrv="pserver1"
$sNewSrv="vhost"
$asPrt  ="6thFloorCanon","6thFloorFieldCrew1","EngineeringPlotter_HPGL2",
	"EngineeringPlotterPS","FIN_P1505N","HP4_2100","HP4_ACR","HP4_Creative",
	"HP4_Engineer","HP4_Exec","HP4_Field","HP4_FINANCE","HP4_Online","HP4_OPS",
	"HP4_Ops2","HP4_Prog1","HP4_Prog2","HP4_Prog3","HP4_Radio","HP4_TapeOps",
	"HP4_VTL","HP5 Graphics","HP5_Aquarius","HP5_HR","HP5_Ops","HP5WJColor",
	"HP_LJ1505","Phaser 8400N PS","RADIO_LJ1022n"

$sDefPath=GetDefaultPrinter()
$sDefSrv=Split($sDefPath+"\\\","\")[2]
$sDefPrt=Split($sDefPath+"\\\","\")[3]
If $iVerbose "[DBG] Default printer server='"+$sDefSrv+"', share='"+$sDefPrt+"'"+@CRLF EndIf

; ***
; Check if the default printer is in the list, AND is mapped to the old server.
; ***
If AScan($asPrt,$sDefPrt)+1 AND $sDefSrv=$sOldSrv
	$iMapped=PriMapState("\\"+$sNewSrv+"\"+$sDefPrt)
	; ***
	; If the printer is not yet mapped to the new server then map it.
	; ***
	If Not $iMapped
		If AddPrinterConnection("\\"+$sNewServer+"\"+$sDefPrt)
			"Could not map printer server='"+$sDefSrv+"', share='"+$sDefPrt+"'"+@CRLF
			"Reason: ["+@ERROR+"] "+@SERROR+@CRLF
		Else
			$iMapped=1
			If $iVerbose "[DBG] Mapped printer server='"+$sDefSrv+"', share='"+$sDefPrt+"'"+@CRLF EndIf
		EndIf
	EndIf

	; ***
	; Only set the default if the printer is mapped and is not already the default.
	; ***
	If $iMapped=1
		If SetDefaultPrinter("\\"+$sNewSrv+"\"+$sDefPrt)
			"Could not set printer "+$sDefPrt+" on server "+$sNewSrv+" as default"+@CRLF
			"Reason: ["+@ERROR+"] "+@SERROR+@CRLF
		Else
			"Printer "+$sDefPrt+" on server "+$sNewSrv+" set as default"+@CRLF
		endif
	EndIf
EndIf

; Lonkero's Shrink-wrapped primter mapping state check funtion.
function PriMapState($_Pri)
if @inwin=1
 if len(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices",$_Pri))
  if split(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),",")[0]=$_Pri
   $PriMapState=2
  else
   $PriMapState=1
  endif
 endif
else
 dim $_Root,$_C,$_C2 $_Root="HKLM\System\CurrentControlSet\control\Print\Printers"
 for $_C=0 to 259
  $_C2=enumkey($_Root,$_C)
  If instr(READVALUE($_Root+"\"+$_C2,"Port"),$_Pri)
   If instr(READPROFILESTRING("%windir%\win.ini","windows","device"),$_Pri)
    $PriMapState = 2
   Else
    $PriMapState = 1
   Endif
  Endif
  if $_C2=259 $_C=$_C2 endif
 next
endif
endfunction

Function GetDefaultPrinter()
	$GetDefaultPrinter = Join(Split(ReadValue("HKEY_USERS\" + @sid + "\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device"), ',', 1), '')
EndFunction

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1376 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.062 seconds in which 0.034 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org