;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       NetPrinters() 
;; 
;;ACTION         Network Printer Management function 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0  - 2010/01/27 
;; 
;;HISTORY        1.0  - 2010/01/27 - Initial Release 
;; 
;;SYNTAX         NetPrinters(printer, [command]) 
;; 
;;PARAMETERS     printer - REQUIRED - String 
;;               - The name of the print share to control/query 
;; 
;;		 command - OPTIONAL - String 
;;               - The task to perform. Task codes are binary and cumulative 
;;		  0 - Query - returns digit: 
;;                    0=Not Connected; 1=Connected; 2=Connected/Default 
;;		  1 - Add - Adds the printer IF it is not already connected 
;;		  2 - Default - Used alone or with Add, it makes the printer 
;;                    the default printer 
;;		  4 - Delete - Deletes the named printer if it exists. 
;;                    Cannot be used with any other value. 
;; 
;;REMARKS        Used in login scripts to efficiently and quickly map printers 
;;		 only when not already connected. Provides a standard interface 
;;               to add, remove, query, and set default status of network printers. 
;; 
;;RETURNS        Int - 1=Success if action, Int - Status if query. 
;;		 When Command is specified and non-zero, returns 1 on success and 0 
;;		 on failure When Command is zero or omitted (Query) returns 0 if 
;;		 named printer is not connected, 1 if connected, and 2 if connected 
;;               and default. The @ERROR macro shoudl be checked on any return of 0 
;;		 to distinguish between "not connected" and an error. 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;; 
;;EXAMPLES        
; 
Function NetPrinters($_Printer, OPTIONAL $_Command)
 
  Dim $_						; temp var 
  Dim $_aPtr						; printer server/device array 
  Dim $_Queue[0]					; queue parameter 
  Dim $_Status						; status value 
  Dim $_oServer						; Server object 
  Dim $_oPrinter					; Printer object 
 
  $NetPrinters = 0					; default return value 
  $_Queue[0] = 'PrintQueue'
 
  $_Command = Val($_Command)				; force numeric 
 
  If $_Command < 0 Or $_Command > 4
    Exit 87						; invalid command - return error 
  EndIf
 
  If Left($_Printer, 2) <> '\\' 			; local printer - can only set default or return status 
    If $_Command = 1 Or $_Command = 4			; cannot add or delete a local printer 
      Exit 87
    EndIf
    $_aPtr = @WKSTA, $_Printer				; element 0 = localhost, 1 = sharename 
  Else							; network printer - set server and share values 
    $_aPtr = Split(SubStr($_Printer, 3), '\')		; element 0 = server, 1 = sharename 
  EndIf		; Printer arg must be a UNC path 
 
  ; instantiate the WinNT resource object 
  $_oServer = GetObject('WinNT://' + $_aPtr[0] + ',computer')
  If @ERROR
    Exit @ERROR
  EndIf
 
  ; determine connection status 
  ;  0 = Not Connected (or error!, check exit code) 
  ;  1 = Connected, not default 
  ;  2 = Connected, Default 
 
  $_Status = 0						; default - not connected/unsuccessful 
 
  $_oServer.Filter = $_Queue   
  For Each $_oPrinter in  $_oServer
    If $_oPrinter.Name = $_aPtr[1]
      If ReadValue('HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices', $_oPrinter.PrinterName)
        If Split(ReadValue('HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows', 'Device'), ',')[0] = $_oPrinter.PrinterName
          $_Status = 2					; connected and default 
        Else
          $_Status = 1					; connected, not default 
        EndIf
        If $_Command = 0				; if simple status query 
          $NetPrinters = $_Status			; set status and 
          Exit 0					; exit now  
        EndIf
      EndIf
 
      If $_Command = 4					; Delete printer 
        If $_Status					; is connected, can be deleted 
          $_ = DelPrinterConnection($_oPrinter.PrinterName)
        EndIf
        $NetPrinters = Not @ERROR
        Exit @ERROR
      EndIf
 
      If $_Command & 1					; connect to printer 
        If Not $_Status
          $_ = AddPrinterConnection($_oPrinter.PrinterName)
          If @ERROR
            $NetPrinters = Not @ERROR			; if error, exit now with status 
            Exit @ERROR
          EndIf						; otherwise drop through to possibly set default  
        EndIf
      EndIf
 
      If $_Command & 2					; set printer as default 
        If $_Status < 2
          $_ = SetDefaultPrinter($_oPrinter.PrinterName)
          $NetPrinters = Not @ERROR			; if error, exit now with status 
          Exit @ERROR
        EndIf						; otherwise drop through to return success 
      EndIf
 
    EndIf
  Next
 
  $NetPrinters = 1					; success 
  Exit 0  
 
EndFunction