从网上找的代码进行了修改!!
其原理1\用注册表添加端口,2\重启打印服务 ,3最后使用"rundll32 printui.dll"命令进行添加打印机
如下:
#RequireAdmin ProgressOn("处理中,请稍候...BY:天祈", "", "", -1, -1, 2 + 16) ProgressSet(0, "已完成 0%", "正在创建变量...") $fileURL=@ScriptDir & "\userprint.ini" ;MsgBox(0, "", $fileURL) If (FileExists($fileURL)==0)Then IniWrite($fileURL,"print","SetPrinterIP","定义打印机IP地址") IniWrite($fileURL,"print","SetPrinterName","定义打印机名称可任意更改") IniWrite($fileURL,"WIN7_32","drvpatch","打印机驱动所在位置") IniWrite($fileURL,"WIN7_32","SetPrinterDriver","定义打印机驱动选择") IniWrite($fileURL,"WIN7_64","drvpatch","") IniWrite($fileURL,"WIN7_64","SetPrinterDriver","") IniWrite($fileURL,"WIN_XP","drvpatch","") IniWrite($fileURL,"WIN_XP","SetPrinterDriver","") Exit EndIf Sleep(300) $SetPrinterIP = IniRead($fileURL,"print","SetPrinterIP","") If $SetPrinterIP="定义打印机IP地址" Then ;确定文件有进行修改 Exit EndIf $SetPrinterName = IniRead($fileURL,"print","SetPrinterName","") ;读打印机名称 If @OSVersion="WIN_7" Then If @OSArch="X86" Then $drvpatch = IniRead($fileURL,"WIN7_32","drvpatch","") ;打印机驱动所在位置 $SetPrinterDriver= IniRead($fileURL,"WIN7_32","SetPrinterDriver","") ;定义打印机驱动选择 Else $drvpatch = IniRead($fileURL,"WIN7_64","drvpatch","") ;打印机驱动所在位置 $SetPrinterDriver= IniRead($fileURL,"WIN7_64","SetPrinterDriver","") ;定义打印机驱动选择 EndIf EndIf If @OSVersion="WIN_XP" Then $drvpatch = IniRead($fileURL,"WIN_XP","drvpatch","") ;打印机驱动所在位置 $SetPrinterDriver= IniRead($fileURL,"WIN_XP","SetPrinterDriver","") ;定义打印机驱动选择 EndIf $Patch = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" $Patch001 = "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports\" $i = 1 $s = "" ProgressSet(10, "已完成 10%", "正在检测端口信息...") Sleep(300) Do $Var = RegEnumKey($Patch, $i) $i = $i + 1 $s = $s & $Var& "/" ProgressSet(10 + $i, "已完成 " & 10 + $i& "%", "正在检测端口信息...") Sleep(300) Until StringLeft($Var, 3) <> "IP_" $x = StringInStr($s, "IP_" & $SetPrinterIP) $PortName = "IP_" & $SetPrinterIP $NewPatch = $Patch & $PortName $NewPatch001 = $Patch001 & $PortName ProgressSet(30, "已完成 30%", "正在检测端口是否存在...") Sleep(300) If $x = 0 Then ProgressSet(40, "已完成 40%", "端口不存在,在正在创建端口...") Sleep(300) RegWrite($NewPatch) RegWrite($NewPatch, "Protocol", "REG_DWORD", "00000001") RegWrite($NewPatch, "Version", "REG_DWORD", "00000001") RegWrite($NewPatch, "HostName", "REG_SZ", "") RegWrite($NewPatch, "IPAddress", "REG_SZ", $SetPrinterIP) RegWrite($NewPatch, "HWAddress", "REG_SZ", "") RegWrite($NewPatch, "PortNumber", "REG_DWORD", "9100") RegWrite($NewPatch, "SNMP Community", "REG_SZ", "public") RegWrite($NewPatch, "SNMP Enabled", "REG_DWORD", "00000001") RegWrite($NewPatch, "SNMP Index", "REG_DWORD", "00000001") ProgressSet(60, "已完成 60%", "端口不存在,正在创建端口...") Sleep(300) RegWrite($NewPatch001) RegWrite($NewPatch001, "Protocol", "REG_DWORD", "00000001") RegWrite($NewPatch001, "Version", "REG_DWORD", "00000001") RegWrite($NewPatch001, "HostName", "REG_SZ", "") RegWrite($NewPatch001, "IPAddress", "REG_SZ", $SetPrinterIP) RegWrite($NewPatch001, "HWAddress", "REG_SZ", "") RegWrite($NewPatch001, "PortNumber", "REG_DWORD", "9100") RegWrite($NewPatch001, "SNMP Community", "REG_SZ", "public") RegWrite($NewPatch001, "SNMP Enabled", "REG_DWORD", "00000001") RegWrite($NewPatch001, "SNMP Index", "REG_DWORD", "00000001") ProgressSet(70, "已完成 70%", "正在停止打印服务...") RunWait(@ComSpec& " /c sc stop spooler", "", @SW_HIDE) ProcessWaitClose ("cmd.exe") ProgressSet(75, "已完成 80%", "正在启动打印服务...") RunWait(@ComSpec& " /c sc start spooler", "", @SW_HIDE) ProcessWaitClose ("cmd.exe") ProgressSet(80, "已完成 80%", "准备添加打印机...") Sleep(300) Else ProgressSet(60, "已完成 60%", "端口已经存在,准备添加打印机...") Sleep(300) EndIf ProgressSet(90, "已完成 90%", "正在添加打印机...") RunWait(@ComSpec& ' /c rundll32 printui.dll,PrintUIEntry /if /b "' & $SetPrinterName& '" /f '& $drvpatch&' /r "' & $PortName& '" /m "' & $SetPrinterDriver& '"', "", @SW_HIDE) ;MsgBox (0,"注意","添加打印机完毕!") Sleep(300) ProgressSet(100, "已完成 100%", "添加打印机完毕!") ProgressOff() MsgBox(0, "", "添加完毕!") Run("RUNDLL32 SHELL32.DLL,SHHelpShortcuts_RunDLL PrintersFolder")