Milas

gui function wonte delete files until gui closes

3 posts in this topic

Hello,

     I am working on a GUI that will perform some reg query and wmic cmds for some of our tier 1 techs. everything so far works well except for my function to get mapped printers from a remote computer. I have the function outputting the list of printers to a text file in the temp folder. at the end of the function its suppose to delete the file and that is where my issue lies. The gui wont delete the file until the GUI itself closes. this is causing an issue where if I want to run the function again the gui shuts down on me.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <inet.au3>
#include <Constants.au3>
#RequireAdmin

#cs
    Program: Phone Tech utilitly tool
    Purpose: Assist phone techs and others with different wmic and reg query cmds to gain information from remote computers
    Creator: Salim C. Proctor
    Co-creator/Editor: John Ennis
    Date Created: 2 Feb 2016
    Last Edited: 16 Feb 2016
    Verion 1.0
#ce


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 627, 459, 192, 114)
$Tab1 = GUICtrlCreateTab(0, 0, 625, 457)

$TabSheet1 = GUICtrlCreateTabItem("Main")
$User = GUICtrlCreateButton("Who's Logged On", 8, 40, 147, 41)
$Host = GUICtrlCreateButton("Get Hostname", 8, 96, 147, 41)
$Acc = GUICtrlCreateButton("User Account Info", 440, 40, 139, 41)
$MAC = GUICtrlCreateButton("Get MAC Address", 8, 152, 147, 41)
$nPrinter = GUICtrlCreateButton("Mapped Printers", 8, 208, 147, 41)
$sUser = GUICtrlCreateInput("", 168, 48, 249, 21)
$sHost = GUICtrlCreateInput("", 168, 104, 249, 21)
$IP = GUICtrlCreateInput("Enter IP/Hostname", 8, 424, 161, 21)
$plist = GUICtrlCreateEdit("", 168, 208, 249, 162)
$sMAC = GUICtrlCreateInput("", 168, 160, 249, 21)
$Ping = GUICtrlCreateButton("Ping", 208, 408, 163, 41)
$mDrive = GUICtrlCreateButton("Remote C$", 440, 408, 147, 41)

$TabSheet2 = GUICtrlCreateTabItem("Program Version")
$search = GUICtrlCreateButton("Search", 8, 32, 147, 41)
$program = GUICtrlCreateInput("Enter Program Name", 176, 40, 321, 21)
$prog_list = GUICtrlCreateEdit("", 8, 96, 609, 318)

GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $User
            remote_user()
        Case $Host
            remote_host()
        Case $MAC
            remote_Mac()
        Case $search
            remote_program()
        Case $Ping
            ping_customer()
        Case $mDrive
            map_drive()
        Case $nPrinter
            get_printers()
        Case $Acc
            Account()
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func remote_user()
    $pid = Run(@ComSpec & " /c wmic /node:" & GUICtrlRead($IP) & " computersystem get username", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

    Local $line
    While 1
        $line &= StdoutRead($pid)
        If @error Then ExitLoop

    WEnd
    While 1
        $line &= StderrRead($pid)
        If @error Then ExitLoop

    WEnd
    $split = StringSplit($line, @CRLF)
    GUICtrlSetData($sUser, $split[4])

EndFunc   ;==>remote_user

Func remote_host()
    $pid = Run(@ComSpec & " /c wmic /node:" & GUICtrlRead($IP) & " computersystem get name", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    Local $line
    While 1
        $line &= StdoutRead($pid)
        If @error Then ExitLoop
    WEnd
    While 1
        $line &= StderrRead($pid)
        If @error Then ExitLoop
    WEnd
    $split = StringSplit($line, @CRLF)
    GUICtrlSetData($sHost, $split[4])

EndFunc   ;==>remote_host

Func remote_Mac()
    $pid = Run(@ComSpec & " /c getmac /nh /s " & GUICtrlRead($IP), @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

    Local $line
    While 1
        $line &= StdoutRead($pid)
        If @error Then ExitLoop

    WEnd
    While 1
        $line &= StderrRead($pid)
        If @error Then ExitLoop

    WEnd
    $split = StringSplit($line, @CRLF)
    $2ndSplit = StringSplit($split[3], "\")

    GUICtrlSetData($sMAC, $2ndSplit[1])



EndFunc   ;==>remote_Mac

Func remote_program()

    Local $fProgram, $nkey, $1position[33], $2position[33]
    Local $pid, $line1[33], $line, $array_size
    GUICtrlSetData($prog_list, "")
    GUICtrlSetState($search, $gui_disable)
    $fProgram = GUICtrlRead($program)
    $pid = Run(@ComSpec & ' /c reg query \\' & GUICtrlRead($IP) & '\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /s /f ' & $fProgram & ' /d| findstr "{"', @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)


    While 1
        $line &= StdoutRead($pid)
        If @error Then ExitLoop
    WEnd
    While 1
        $line &= StderrRead($pid)
        If @error Then ExitLoop
    WEnd

    $nkey = StringSplit($line, @CRLF & @CRLF)
    $array_size = UBound($nkey)
    ReDim $1position[$array_size]
    ReDim $line1[$array_size]


    For $i = 1 To $array_size - 1
        $1position[$i] = StringLen($nkey[$i])

        If $1position[$i] < 132 Then
            $pid = Run(@ComSpec & ' /c reg query \\' & GUICtrlRead($IP) & '\' & $nkey[$i] & '| findstr /i "displayname displayversion"', @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)


            While 1
                $line1[$i] &= StdoutRead($pid)

                If @error Then ExitLoop
            WEnd

            GUICtrlSetData($prog_list, $line1[$i] & @CRLF & "~", "~")

        EndIf

        $i = $i + 1

    Next

    GUICtrlSetState($search, $gui_enable)
EndFunc   ;==>remote_program

Func ping_customer()
    Local $error

    If GUICtrlRead($IP) <> "" Then
        $pError = RunWait("ping.exe -n 1 " & GUICtrlRead($IP), "", @SW_HIDE)

        Switch $pError
            Case 0
                MsgBox(0, "Successfull", "Operation was a success!")
            Case 1
                MsgBox(48, "ERROR CODE: 001", "Host/IP is not reachable")
        EndSwitch
    Else
        MsgBox(48, "ERROR CODE: 005", "No input detected")
    EndIf
EndFunc   ;==>ping_customer

Func map_drive()
    ShellExecute("C:\Users\Salim.Proctor\Desktop\GUI\john project\c$ remote.exe")
EndFunc   ;==>map_drive

Func get_printers()
    Local $pid, $sid, $sid2, $hfileopen, $sfileread, $line, $exit, $ip45, $user45

    ;disables map printer button in gui
    GUICtrlSetState($nPrinter, $gui_disable)

    ;clears any previous data in the printer edit box
    GUICtrlSetData($plist, " ")
    
    ;reads ip address user entered
    $ip45 = GUICtrlRead($IP)
    
    ;reads the user name from the sUser input box
    $x = GUICtrlRead($sUser)
    
    ;splits the data to remove domainname\
    $y = StringSplit($x, "\")
    
    ;removes a space value from the data
    $user45 = StringSplit($y[2], " ")

    ;queries the remote users registry to find thier sid
    $pid = Run(@ComSpec & ' /c reg query "\\' & $ip45 & '\hklm\software\microsoft\windows nt\currentversion\profilelist" /s /f "' & $user45[1] & '"', @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

    ;reads the standard ouput from the reg query
    While 1
        $line &= StdoutRead($pid)
        If @error Then ExitLoop
    WEnd
    While 1
        $line &= StderrRead($pid)
        If @error Then ExitLoop
    WEnd
    ;string splits to remove all unneeded data and get only the sid
    $sid = StringSplit($line, "\")
    $sid2 = StringSplit($sid[7], @CRLF)

    ;queries the remote users registry using the newly obtained sid and writes it out to a text file
    $pid = Run(@ComSpec & ' /c reg query \\' & $ip45 & '\hkey_users\' & $sid2[1] & '\Printers\Connections > c:\temp\printer_list.txt', @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

    ;pause program to give it time to create the file
    Sleep(1000)

    ;opens the file so that it can be read
    If FileExists("c:\temp\printer_list.txt") Then
        FileWriteLine("c:\temp\printer_list.txt", "1\2\3\4\end of list")
        $hfileopen = FileOpen("c:\temp\printer_list.txt")
    EndIf

    $exit = " "
    $i = 2

    ;output the data into the gui until it reaches the end of the list
    Do
        $exit = FileReadLine("c:\temp\printer_list.txt", $i)
        $printer_string = StringSplit($exit, "\")
        GUICtrlSetData($plist, $printer_string[5] & @CRLF & "~", "~")
        $i = $i + 1

    Until $exit = "1\2\3\4\end of list"

    ;reeneables the gui map printer button
    GUICtrlSetState($nPrinter, $gui_enable)

    ;delete the text file that is created by the command line
    If FileExists("c:\temp\printer_list.txt") Then
        FileClose("c:\temp\printer_list.txt")
        FileDelete("c:\temp\printer_list.txt")

    EndIf

EndFunc   ;==>get_printers

Func Account()
    Local $line
    $split = StringSplit(GUICtrlRead($sUser), "\")

    $pid = Run(@ComSpec & " /c net user " & $split[2] & " /domain", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

    While 1
        $line &= StdoutRead($pid)
        If @error Then ExitLoop

    WEnd

    While 1
        $line &= StderrRead($pid)
        If @error Then ExitLoop

    WEnd
    MsgBox(0, "STDOUT read:", $line)
EndFunc   ;==>Account

just to reiterate the function im having issue with is func get_printers

Share this post


Link to post
Share on other sites



Line 247, FileOpen is blocking the deletion, and handle is never used.

1 person likes this

Share this post


Link to post
Share on other sites

autoBert

it is just as you said. thank you! I cant believe I didn't see that.

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

  • Similar Content

    • Miliardsto
      By Miliardsto
      Hello.
      Drag option works only on parent window but as we can see on the parent gui is showed up child gui (on the right side with the color).
      Desired action is when we want to drag whole program we can also drag child gui and child gui will drag with parent gui.
      How to do this?
       
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3> GUIRegisterMsg($WM_NCHITTEST, "_NCHITTEST") $hGUI = GUICreate("Test", 500, 500, -1, -1) $cButton_0 = GUICtrlCreateButton("Child 0", 10, 10, 80, 30) $cButton_1 = GUICtrlCreateButton("Child 1", 10, 50, 80, 30) $cButton_2 = GUICtrlCreateButton("Child 2", 10, 90, 80, 30) GUISetState(@SW_SHOW, $hGUI) ; guis Global $hChild_[3] Global $countGUI = UBound($hChild_) - 1 ;MsgBox("","",$countGUI) ; licznik dla GUI Global $gui = 0 $hChild_[0] = GUICreate("Child 0", 350, 300, 150, 0, $WS_POPUP, $WS_EX_MDICHILD, $hGUI) GUISetBkColor(0xFF0000) GUISetState(@SW_SHOW, $hChild_[0]) $hChild_[1] = GUICreate("Child 1", 350, 300, 150, 0, $WS_POPUP, $WS_EX_MDICHILD, $hGUI) GUISetBkColor(0x00FF00) GUISetState(@SW_HIDE, $hChild_[1]) $hChild_[2] = GUICreate("Child 2", 350, 300, 150, 0, $WS_POPUP, $WS_EX_MDICHILD, $hGUI) GUISetBkColor(0x0000FF) GUISetState(@SW_HIDE, $hChild_[2]) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cButton_0 _All_Hide() GUISetState(@SW_SHOW, $hChild_[0]) $gui = 0 Case $cButton_1 _All_Hide() GUISetState(@SW_SHOW, $hChild_[1]) $gui = 1 Case $cButton_2 _All_Hide() GUISetState(@SW_SHOW, $hChild_[2]) $gui = 2 EndSwitch WEnd Func _All_Hide() GUISetState(@SW_HIDE, $hChild_[0]) GUISetState(@SW_HIDE, $hChild_[1]) GUISetState(@SW_HIDE, $hChild_[2]) EndFunc Func _NCHITTEST($hWnd, $uMsg, $wParam, $lParam) If $hWnd = $hGUI Then Local $aPos = WinGetPos($hWnd) If Abs(BitAND(BitShift($lParam, 16),0xFFFF)- $aPos[1]) < 500 Then Return $HTCAPTION EndIf Return $GUI_RUNDEFMSG EndFunc  
    • IAMK
      By IAMK
      Hello,
       
      I have a script which uses a GUI with two buttons. It gets shown and hidden., and pressing the buttons (including their hotkeys) highlights the button, making you able to press enter the next time.
      However, I don't want that.
       
      I've tried using WinActivate() to make enter do things on another program, but the enter is still tied to the AutoIT GUI (which still has the button highlighted).
       
      Here's the important part of my code:
      $MY_GUI = GUICreate("Check.exe", 400, 300, ((@DesktopWidth / 2) - 200), -500) WinSetOnTop($MY_GUI, "", 1) ;Keeps the window on the top. $Button1 = GUICtrlCreateButton("Pass (Ctrl+P)", 40, 250, 120, 40, $WS_GROUP) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") $Button2 = GUICtrlCreateButton("Fail (Ctrl+F)", 240, 250, 120, 40, $WS_GROUP) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") $GUIText = GUICtrlCreateLabel("", 20, 10, 360, 230) GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif") GUICtrlSetData($GUIText, Execute($stepMessage[$step])) $showGUI = "True" GUISetState(@SW_SHOW) While($showGUI = "True") $buttonPressed = GUIGetMsg() Switch $buttonPressed Case $Button1 ;Pass $pass = "True" $buttonPressed = 0 $showGUI = "False" GUISetState(@SW_HIDE) Case $Button2 ;Fail $buttonPressed = 0 $showGUI = "False" GUISetState(@SW_HIDE) While($message = "") $message = InputBox("Check.exe", "Please enter the reason the step failed." & @CRLF, "", "", 300, 200, ((@DesktopWidth / 2) - 150), -400) WEnd Return $message EndSwitch WEnd Local $pass = "False" ;Whether "Pass" or "Fail" was pressed. Local $failLog = xxxfunc($pass) ;Returns a message to log if the step failed. ##THIS CALLS THE CODE SNIPPET ABOVE## FileWrite($file, _Now() & ": Step " & $step & getStatus($pass) & "and took " & Ceiling(TimerDiff($timer) / 1000) & " seconds." & @CRLF) If($failLog <> "") Then FileWrite($file, "Logged comment: " & $failLog & @CRLF) EndIf WinActivate("[TITLE:ProgramName]") ;Hotkeys: HotKeySet("^{p}", "pressPass") HotKeySet("^{f}", "pressFail") Func pressPass() $buttonPressed = $Button1 EndFunc Func pressFail() $buttonPressed = $Button2 EndFunc  
      The GUI's button/hotkey pressing works correctly.
      The same WinActivate code works in another script of mine.
      Together, WinActivate is not take "Active" away from the GUI's last-pressed button. How do I fix this?
       
      Thank you in advance.
    • fatpig
      By fatpig
      Dear AutoIT Community.
      I am currently working on a program that will display lots of images in a scroll down GUI.
      I create the GUI based on the number of images, which works fine.
      But all images I place using GuiCtrlCreatePic above a threshold of around 32000 pixels (cant say exactly) are placed at negative locations and finally will reach 0 again
      and will then populate on top of the images placed there before.
      For $i = 1 To $Images[0] ; Get picture dimensions $hImage = _GDIPlus_ImageLoadFromFile($ImgRep & $Images[$i]) $iX = _GDIPlus_ImageGetWidth($hImage) $iY = _GDIPlus_ImageGetHeight($hImage) _GDIPlus_ImageDispose($hImage) ; Resize & place images in GUI $Pic[$i] = GUICtrlCreatePic($ImgRep & $Images[$i], $Spacer, Round($yPos, 0), $MaxImgWidth, $MaxImgWidth * ($iY / $iX)) $yPos = $yPos + $MaxImgWidth * ($iY / $iX) + $Spacer $log = $log & "|" & $yPos $currentlut = $Images[1] Next I have found a limit at 32767 pixels for some GUI elements when googling around.
      Can someone tell me how to circumvent that?
      thanks!
      cheers
      Felix
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Guys this is my gui and i need the numeric value typed in the highlighted  inputbox. if this value is greater than 1 i need to perform some operations
      \
       
      Here is the summary about that field which i got from wininfo tool

      i tried writing the code as shown below.. but the msgbox is popping up empty value..when the same code is pasted under some button action i am able to access the value present in the field..but when placed alone as written in the attached script i am unable to fetch it... 

      here i am using the valu present in the field

      can you guys tell me what's wrong??? or how can i get that value?
       
      My_GUI.au3

    • Neonovaz
      By Neonovaz
      Hello
       
      Is there anyway to store word documents in Autoit GUI? For example I have a instruction sheet that I want to bundle up with the exe.

      So a user simply clicks the icon and the stored document will launch  (Something like how you can add objects like excel sheets in word documents )

      (I Know we can launch word files from script directory)