Sign in to follow this  
Followers 0
Akarillon

Adding a new printer, AutoIT bugs[Solved]

8 posts in this topic

#1 ·  Posted (edited)

I am trying to add a printer via the network share \\winprint($OS)\$printername

But it seems like when the program gets to RegRead(to check if the printer is correctly added) it stops working and I have to exit the script by force.

The program running got to be exited via the task manager(Autoit3Wrapper.exe*32)

Really need this to work =)

$CurrentOS = 0
$Printer = 0
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Start
   $Printer = GUICtrlRead($Input)
   If GUICtrlRead(7) = "Windows XP" Then
    $CurrentOS = 2
   ElseIf GUICtrlRead(7) = "Windows Vista" Then
    $CurrentOS = 2
   ElseIf GUICtrlRead(7) = "Windows 7" Then
    $CurrentOS = 64
   EndIf
   _RunDOS('start \\winprint' & $CurrentOS & '\' & $Printer & '')
   RegRead('HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices', '\\winprint' & $CurrentOS & '\' & $Printer & '')
   If @error Then
    MsgBox(0, "Error", 'Error' & @error & ' appeared.' & @CRLF & 'Please contact edb@kjemi.uio.no.')
   Else
    MsgBox(0, "Done", "Printer should have been added successfully!")
   EndIf
  Case $Exit
   Exit
EndSwitch
WEnd

Kind Regards,

Akarillon

Edited by Akarillon

Challenge accepted!

Share this post


Link to post
Share on other sites



no.. the line should say exactly:

\\winprint64\kj_vu18

This is an example of the printer kj_vu18 on a server that does 64bit drivers


Challenge accepted!

Share this post


Link to post
Share on other sites

I found the error I think.

When I go into the task manager I can end 2 processes.

And 1 is the AutoIt3Wrapper.exe*32 and the other is cmd.exe

When I ended cmd.exe before AutoIt3Wrapper.exe*32 the script finished.

So is it safe(stable) to add a ProcessEnd in the middle of the script?


Challenge accepted!

Share this post


Link to post
Share on other sites

I have a old script that may give you some ideas.

#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Process.au3>
#include <Array.au3>
$g_szVersion = "PrintInst.exe"
If WinExists($g_szVersion) Then Exit
Opt("TrayIconDebug", 1) ;debug is on

Dim $hService, _
        $data, _
        $sPrinter, _
        $sPrinterList, _
        $font = "Comic Sans MS", _
        $Input[12], $Combo[7], $radio[7], _
        $check[11], $Button[6]

Global Enum Step + 25 _
        $CON_POS_1 = 25, _
        $CON_POS_2, _
        $CON_POS_3, _
        $CON_POS_4, _
        $CON_POS_5, _
        $CON_POS_6, _
        $CON_POS_7, _
        $CON_POS_8, _
        $CON_POS_9, _
        $CON_POS_10, _
        $CON_POS_11

GUICreate("", 600, 290, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
GUICtrlCreateLabel("\\", 20, 54, 10, 20)
$iniread_1 = IniRead(@ScriptDir & "/npu.ini", "SERVER", "SERVER", "")
$iniread_2 = IniRead(@ScriptDir & "/npu.ini", "Printers", "PLIST", "")
iniwrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "")
$iniread_3 = IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "")
$LocalPrinters = StringSplit($iniread_3, "|")
If $LocalPrinters[0] > 10 Then
    ReDim $LocalPrinters[11]
EndIf

$Input[1] = GUICtrlCreateInput($iniread_1, 35, 50, 100, 20)

For $i = 2 To 11
    $Input[$i] = GUICtrlCreateInput("", 270, Eval("CON_POS_" & $i - 1), 270, 21)
    If ($i - 1) <= $LocalPrinters[0]Then
        GUICtrlSetData(-1, $LocalPrinters[$i - 1])
    EndIf
Next

For $i = 1 To 6
    $Combo[$i] = GUICtrlCreateCombo("", 20, Eval("CON_POS_" & $i + 2), 170, 21)
    GUICtrlSetData(-1, $iniread_2)
    $radio[$i] = GUICtrlCreateRadio("", 210, Eval("CON_POS_" & $i + 2), 15, 21)
Next

For $i = 1 To 10
    $check[$i] = GUICtrlCreateCheckbox("", 550, Eval("CON_POS_" & $i), 20, 20)
Next

GUICtrlCreateLabel("Currently install printers", 270, 3, 200, 21)
GUICtrlCreateLabel("Delete", 550, 7, 100, 21)
GUICtrlCreateGroup("Default", 200, 62, 50, 160)

$Button[1] = GUICtrlCreateButton("Run", 20, 225, 35, 30)
$Button[2] = GUICtrlCreateButton("Exit", 65, 225, 35, 30)
$Button[3] = GUICtrlCreateButton("Find", 146, 50, 43, 20)
$Button[4] = GUICtrlCreateButton("Find", 498, 3, 43, 20)
$Button[5] = GUICtrlCreateButton("Restart Spooler", 110, 225, 80, 30)

$Label_1 = GUICtrlCreateLabel("   Network Printer Utility" & @CRLF & "for Windows Print Servers", 20, 0, 240, 50)
GUICtrlSetFont(-1, 10, 400, "", $font)
$Label_2 = GUICtrlCreateLabel("2006 CTS Scripting Team", 40, 270, 130, 25)
$_dummy = GUICtrlCreateDummy()

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Button[1]
            _Rprinter()
            _instprint()
            MsgBox(0, "", "Done!", 3)
        Case $Button[2]
            ExitLoop
        Case $Button[3]
            $data = GUICtrlRead($Input[1], 0)
            _netprint1()
        Case $Button[4]
            _getinstalledPrinters()
        Case $Button[5]
            _restart()
        Case Else
            ;;;
    EndSwitch
WEnd

Exit

Func _netprint1()
    $hService = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & "\\" & $data & "\root\cimv2")
    If @error Then
        MsgBox(48, "ERROR", "No Printers Found. Possible issues: " & @CRLF _
                 & "" & @CRLF _
                 & "  1. The Windows Print Server name has been entered in incorrectly." & @CRLF _
                 & "  2. You are trying to access a Novell Server. This utility does not support Novell Print Servers." & @CRLF _
                 & "  3. There are no printers shared on the Windows Print Server you selected.")
    Else
        _np2()
    EndIf
EndFunc   ;==>_netprint1

Func _np2()
    Local $i = 0
    Local $sPrinters = ""
    Local $aArray[1]

    SplashTextOn("", "Gathering printers, please wait...", 150, 75)

    For $y = 1 To 6
        GUICtrlSetData($Combo[$y], "")
    Next
    IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", "")

    $oPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")

    For $oPrinter In $oPrinterList
        ReDim $aArray[$i + 1]
        $aArray[$i] = StringUpper($oPrinter.name)
        $i += 1
    Next

    _ArraySort($aArray)

    For $i = 0 To UBound($aArray)-1
        For $y = 1 To 6
            GUICtrlSetData($Combo[$y], $aArray[$i])
        Next
    Next

    $iElements = UBound($aArray) - 1
    For $x = 0 To $iElements
        If $x = $iElements Then
            $sPrinters &= $aArray[$x]
        Else
            $sPrinters &= $aArray[$x] & "|"
        EndIf
    Next

    IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", $sPrinters)
    $dataget2 = GUICtrlRead($Input[1], 0)
    IniWrite(@ScriptDir & "/npu.ini", "SERVER", "SERVER", $dataget2)
    SplashTextOn("", "Select what printers you wish to install", 150, 75)
    Sleep(2000)
    SplashOff()
EndFunc   ;==>_np2

Func _instprint()
    Local $ComboGet[7], $RadioGet[7]

    SplashTextOn("", "Installing printer, please wait...", 150, 75)

    $InputGet = GUICtrlRead($Input[1], 0)

    For $i = 1 To 6
        $ComboGet[$i] = GUICtrlRead($Combo[$i], 0)
        $RadioGet[$i] = GUICtrlRead($radio[$i], 0)
    Next

    For $i = 1 To 6
        If $ComboGet[$i] <> "" Then
            RunWait(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /c\\" & @ComputerName & " /n\\" & $InputGet & "\" & $ComboGet[$i])
        EndIf
    Next
    _RunDOS("NET STOP spooler")
    _RunDOS("NET START spooler")
    sleep(10000)
    For $i = 1 To 6
            If $RadioGet[$i] = 1 Then
                If $ComboGet[$i] <> "" Then
                RunWait(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n\\"&$iniread_1&"\"& $ComboGet[$i])
                endif
            EndIf
            GUICtrlSetData($Combo[$i],"")
            GUICtrlSetState($radio[$i], $GUI_UNCHECKED)
            sleep(50)
            GUICtrlSetData($Combo[$i],$iniread_2)
    Next
    SplashOff()
EndFunc   ;==>_instprint

func _restart()
    SplashTextOn("", "Stopping Print Spooler, please wait...", 150, 75)
    _RunDOS("NET STOP spooler")
    SplashOff()
    SplashTextOn("", "Starting Print Spooler, please wait...", 150, 75)
    _RunDOS("NET START spooler")
    SplashOff()
EndFunc

Func _getinstalledPrinters()

    SplashTextOn("", "Gathering installed printers, please wait...", 200, 75)

    IniWrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "")
    $hService = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & "\\" & @ComputerName & "\root\cimv2")
    $sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")
    ConsoleWrite('$sPrinterList.count = ' & $sPrinterList.count & @LF)
    For $sPrinter In $sPrinterList
        ConsoleWrite('$sPrinter.name = ' & $sPrinter.name & @LF)
        $rd3 = IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "")
        IniWrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", $rd3 & $sPrinter.name & "|")
    Next
    $local = StringSplit(IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", ""), "|")
    If UBound($local) < 11 Then ReDim $local[11]
    For $i = 1 To 10
        GUICtrlSetData($Input[$i + 1], $local[$i])
    Next

    SplashOff()
EndFunc   ;==>_getinstalledPrinters

Func _Rprinter()
    Local $InputGet[11]
    For $i = 2 To 11
        $InputGet[$i - 1] = GUICtrlRead($Input[$i])
    Next
    For $i = 1 To 10
        If GUICtrlRead($check[$i]) = 1 Then
            SplashTextOn("", "Removing printer, please wait...", 150, 50)
            GUICtrlSetState($check[$i], $GUI_UNCHECKED)
            sleep(1000)
            If $InputGet[$i] <> "" Then
                $str = StringLeft($InputGet[$i], 2)
                If $str = "\\" Then
                    RunWait(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /gd /dn /n /c\\" & @ComputerName & " /n" & $InputGet[$i])
                    ;RunWait(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /gd /q /c\\" & @ComputerName & " /n" & $InputGet[$i]) ;mine original
                    ;RunWait(@ComSpec & ' /c RUNDLL32 PRINTUI.DLL,PrintUIEntry /gd /dn /q /n "' & $ret & '"', '', @SW_HIDE) ;danny35
                    GUICtrlSetData($InputGet[$i], "")
                Else
                    RunWait(@SystemDir & '\RUNDLL32 PRINTUI.DLL,PrintUIEntry /dl /q /n "' & $InputGet[$i] & '" /c\\' & @ComputerName)
                EndIf
            EndIf
        EndIf
    Next

EndFunc   ;==>_Rprinter

Share this post


Link to post
Share on other sites

Thanks :graduated:

I'll check it out and post if it worked out =)


Challenge accepted!

Share this post


Link to post
Share on other sites

I am not that good in AutoIT yet, but I still got a few hints out from your script, so mine works now =)

Thanks again!


Challenge accepted!

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

  • Similar Content

    • Byte11
      By Byte11
      WinSetState ("[CLASS:OpusApp]", "", @SW_SHOWMAXIMIZED) and WinSetState ("[CLASS:OpusApp]", "", @SW_MAXIMIZE) don't maximize the window.  Using WinSetState("[CLASS:OpusApp]","",@SW_RESTORE) allows me to maximize it about 15% of the time. I've tried using  WinMove("[active]",0,0,@Desktopwidth,@Desktopheight), but that only makes it worse. I've also tried putting in a sleep and maximizing it again. Does anyone have a solution?
      Here's my code:
      The startup script is intentionally commented, because I'm not testing it right now. The other comment is just for debugging when I need it. 
       
      UPDATE: When I run two instances of the program, it works almost all the time, but if I copy the code inside the else statement twice it doesn't work at all (despite the fact that it's running the same code just in one program vs two). I also noticed that when I run two instances of it, about 50% of the time when it opens both the windows, it also hits the windows button and types the letter d into the search box. Neither of these actions are in my code. I'm not really sure what's going on. 
    • distancesprinter
      By distancesprinter
      _ArrayDisplay($aArray, "Window Title", "1:", 0, Default, "Column") ; Expected results are rows 1 to the end of the array, all columns. The result is rows 0-1, all columns. The API reference is here:
      https://www.autoitscript.com/autoit3/docs/libfunctions/_ArrayDisplay.htm
       
      Am I doing something wrong?
    • imitto
      By imitto
      Hi!
      I just playing around with _ArrayMin for my next project, but seems like it's not working. I think the code is OK, but I always get the value of $aArray[2][1], not col 2's lowest value (see attached image)

    • nss
      By nss
      Hi all.
       
      Because of me wondering if I could access the key/value pair arrays with the numbers as indexes, I have found out that the zeroth element for some reason doesn't return anything.
      Here's the example:
       
      local $r[2] $r["test1"]="hello" $r["test2"]="how are you" msgbox(64, $r[0], $r[1]) ; prints the ["test2"] but not ["test1"]. Is this even supposed to be a thing?  
       
      BTW, I haven't seen Autoit get updated since 2015; is it abandened or something?
      Any help/clarification appreciated.
    • jguinch
      By jguinch
      Here is an UDF for managing printers.
      Features are :
       - add, remove or rename a printer
       - add or remove a driver
       - add or remove a TCP/IP printer port
       - add or remove a LPR printer port
       - connect to a remote printer
       - enum printers and there configuration and properties
       - pause resume or cancel all jobs of a printer
       - checks if a printer exists
       - print a test page
       - set the default printer