;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       AddPrinter1() 
;; 
;;ACTION         Add IP,LPT, or COM Printers locally or remotely.  
;;               Also, add PrintServer connections locally. 
;; 
;;AUTHOR         Allen Powell 
;; 
;;CONTRIBUTORS   Glenn Barnas 
;; 
;;VERSION        1.2.0 - 2007/01/26 
;; 
;;HISTORY        1.0.0 - 2003/05/07 - Initial Release 
;;               1.1.0 - 2006/11/17 - Support for Novarsinstrings, All Vars Dimmed, 
;;                                    Cleaned up code  
;;               1.1.1 - 2006/12/16 - Fixed Code when adding network printers  
;;               1.2.0 - 2007/01/26 - Added Support for LPR Printers 
;; 
;;SYNTAX         AddPrinter(Port,PrinterType,[DriverInf],[PrinterDesc],[remotepc],[Default],[LPR]) 
;; 
;;PARAMETERS     Port - REQUIRED    
;;               - IPAddress(RAW)   (xxx.xxx.xxx.xxx)  
;;               - IPAddress(LPR)   (xxx.xxx.xxx.xxx:LPRQueueName) 
;;               - LPT              (LPT1:)  
;;               - COM              (COM1:)  
;;               - PrintServer      (\\server\printer)  
;;               PrinterType - REQUIRED 
;;               - Descriptor of Printer within INF file  
;;               - Note:  If using a PrintServer, $PrinterType value is ignored.  
;;               DriverInf - OPTIONAL 
;;               - Location and name of INF file containing print drivers  
;;               - Note:  If omitted, attempts to use default Windows Print Drivers  
;;               PrinterDesc - OPTIONAL 
;;               - Description of Printer  
;;               - Note:  If ommitted, $PrinterDesc = PrinterType  
;;               RemotePC - OPTIONAL 
;;               - PC to install Printer/Drivers on.  If omitted defaults to localpc.  
;;               - Note:  Ignored if $Port is a PrintServer  
;;               Default - OPTIONAL 
;;               - 0 = Do not Set as Default Printer (Default)  
;;               - 1 = Set as Default Printer 
;; 
;;REMARKS         
;; 
;;RETURNS        Integer - status (1=Fail) 
;;               1 - Failed  
;;               If Failed check @error  
;;                see below for custom errors  
;;               -4 - Error connecting to RemotePC  
;;               -3 - Could not open/find INF file  
;;               -2 - Could not find $PrinterType in INF file    
;;               -1 - Unrecognized Port  
;; 
;;DEPENDENCIES    
;;              Windows 2000 Professional and up  
;;              rundll32 printui.dll,PrintUIEntry 
;; 
;;TESTED WITH    W2K3, W2K8, W2K12 
;; 
;;EXAMPLES     
;;               $=AddPrinter("192.168.1.1","Xerox DocumentCenter 555 PCL6","\\Server\PrinterDrives\Xerox\dc555pcl\dc555x.inf","Xerox Document Centre 55  5 PCL6 in Office",REMOTEPC,1)  
;;               $=Addprinter("LPT1","HP LaserJet 8100 Series PS",,"HP8100 in IS(test)")  
;;               $=AddPrinter("192.168.1.1","HP LaserJet 8100 Series PS",,"HP8100 in IS(test)",,1)  
;;               $=AddPrinter('\\server\printer','_')  
;; 
; 
function AddPrinter1($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default)
    Dim $found,$IPPrinterKey,$PrinterCMD,$ffh,$,$spooler,$services,$line,$objservice,$printserver,$LPRQueue
    $AddPrinter1=1
    if $Driverinf=""
      $DriverInf="%windir%\inf\ntprint.inf"
    endif
    if exist($DriverInf)=0
      Exit -3
    endif
    shell '"%comspec%" /c type "' + $driverinf + '">"%temp%\addprinter.txt"'
    $ffh=freefilehandle()
    if open($ffh,"%temp%\addprinter.txt",0)=0
      $found=0
      $line=readline($ffh)      
      while @error=0 and $found=0
        $found=instr($line,$PrinterType)
        $line=readline($ffh)
      loop
      $=close($ffh)
      if $found=0
        exit -2
      else
        if $PrinterDesc=""
          $PrinterDesc=$PrinterType
        endif
        if $remotepc=""
          $remotepc='\\'+ @wksta
        else 
          if left($remotepc,2)<>"\\"
            $remotepc='\\' + $remotepc
          endif
        endif
        select 
          case left($Port,2)="\\"
            $printserver=1
            $printerdesc=$port
          case instr($Port,".")
            if instr($Port,":")
              $Port=split($port,":")[0]
              $LPRQueue=split($port,":")[1]
            endif
            $IPPrinterKey = $remotepc + '\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\IP_' + $Port
            if keyexist($IPPrinterKey)=0
              If $LPRQueue ; LPR 
                if WriteValue($IPPrinterKey,"Protocol","00000002","REG_DWORD") exit @error endif
                if WriteValue($IPPrinterKey,"Version","00000001","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"Double Spool","00000001","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"Queue",$LPRQueue, "REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"HostName","", "REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"IPAddress",$Port,"REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"HWAddress","","REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"PortNumber","00000203","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Community","public","REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Enabled","00000001","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Index","00000001","REG_DWORD")  exit @error endif
              Else ; RAW 
                if WriteValue($IPPrinterKey,"Protocol","00000001","REG_DWORD") exit @error endif
                if WriteValue($IPPrinterKey,"Version","00000001","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"HostName","", "REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"IPAddress",$Port,"REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"HWAddress","","REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"PortNumber","00009100","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Community","public","REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Enabled","00000001","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Index","00000001","REG_DWORD")  exit @error endif
              endif
            endif
            $objservice=Getobject("WinNT://" + substr($remotepc,3) + "/Spooler,Service")
            if @error
              exit -4
            endif
            $objservice.stop
            do
              sleep 0.2
            until $objservice.status=1   ;stopped  
            $objservice.start
            $port='IP_' + $port
          case ucase(left($Port,3))="LPT" or ucase(left($Port,3))="COM"
            if right($port,1)<>":"
              $Port=$port+":"
            endif
            if val(left(right($port,2),1))=0
              exit -1 
            endif
          case ucase(left($Port,4))="FILE"
            if right($port,1)<>":"
              $Port=$port+":"
            endif
          case 1
            exit -1
        endselect
        if $Printserver
          if addprinterconnection($port)
            exit @error    
          endif
        else
          $printercmd='rundll32 printui.dll,PrintUIEntry /if /b "' + $printerdesc + '" /c "' + $remotepc + '" ' + 
                      '/f "' + $driverinf + '" /r "' + $port + '" /m "' + $Printertype + '" /z /u '
          Shell $printercmd
        endif
        if $default
          if SetDefaultPrinter($PrinterDesc)
            exit @error
          endif
        endif 
        $AddPrinter1=0
      endif
    else
      Exit -3
    endif
endfunction