Jump to content

need help for fix my script


Recommended Posts

Anyone please advise me what is going wrong with my script.

my script will run normally if i didnt use GUI. please see my script below for detail :

 

#RequireAdmin

Global $file, $file2, $time, $exit, $second, $first
Global $NAC, $NA

;-------------------------------------------------------------------------------------------;

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPIDiag.au3>
#include <TrayConstants.au3>
#include <MsgBoxConstants.au3>

;--------------------Script After WhileEnd wont work if i use this GUI, anyone please help me ??---------------------------------
$Form1 = GUICreate("Form1", 387, 80, 188, 138)
$Label1 = GUICtrlCreateLabel("Default Gateway", 24, 16, 83, 17)
$Label2 = GUICtrlCreateLabel("MAC Address", 24, 48, 68, 17)
$Label3 = GUICtrlCreateLabel(":", 112, 16, 7, 17)
$Label4 = GUICtrlCreateLabel(":", 112, 48, 7, 17)
$Label5 = GUICtrlCreateLabel("", 136, 16, 132, 20)
$Label6 = GUICtrlCreateLabel("", 136, 48, 132, 20)
$button1 = GUICtrlCreateButton("Add To Startup", 288, 8, 81, 65)
GUISetState(@SW_SHOW)


if RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO") then
        GUICtrlSetData($button1,"Kill From Startup")
EndIf

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
            FileDelete("D:\ORIGINAL.txt")
            FileDelete("D:\update.txt")
            Exit
    Case $button1
        If RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO") = "" then
                GUICtrlSetData($button1,"Kill From Startup")
                AddStartup()
            Else
                RemoveStartup()
                GUICtrlSetData($button1,"Run On Startup")
        EndIf

    EndSwitch
WEnd

;-----------------the below script wont work anymore if i use above GUI---------------------------------

$Services = ObjGet("winmgmts:\\.\root\CIMV2")
$NACs = $Services.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True')
For $NAC In $NACs
     $NAs = $Services.ExecQuery('SELECT * FROM Win32_NetworkAdapter')
     For $NA In $NAs
     If  $NA.MACAddress = $NAC.MACAddress  Then
        Exitloop 2
     EndIf
Next
Next



If _WinAPI_IsNetworkAlive() Then

    $comand=" /c arp "
    $comand=$comand&$NAC.DefaultIPGateway(0)&" -g>D:\ORIGINAL.txt"
    $cmd = Run(@Comspec & $comand, "", @SW_HIDE)

    $comand=" /c arp "
    $comand=$comand&$NAC.DefaultIPGateway(0)&" -g>D:\UPDATE.txt"

    DO
        $cmd = Run(@Comspec & $comand, "", @SW_HIDE)

        sleep ($time)

        $file=FileOpen("D:\Update.txt",0)
        $first=FileReadLine($file, 4)
        $first=StringTrimRight($first,15)
        $first=StringTrimLeft($first,24)
        $file=FileClose("D:\Update.txt")

        $file2=FileOpen("D:\ORIGINAL.txt",0)
        $second=FileReadLine($file2, 4)
        $second=StringTrimRight($second,15)
        $second=StringTrimLeft($second,24)
        $file2=FileClose("D:\ORIGINAL.txt")

        if $first=$second Then
            $time=5000
        Else
            MsgBox(0,"DETECTION", "YOUR MAC ADDRESS WAS CHANGED, TRYING TO RESOLVING")
            $comand=" /c netsh -c ""interface ipv4"" set neighbors "&$NA.NetConnectionID&" "&$NAC.DefaultIPGateway(0)&" "&$second&" "
            $cmd = Run(@Comspec & $comand, "", @SW_HIDE)
            sleep(5000)
            $comand=" /c arp "
            $comand=$comand&$NAC.DefaultIPGateway(0)&" -g>D:\UPDATE.txt"
        EndIf


    until $exit
Else
        TrayTip("","No Connection, unable to start this application",5)
        Exit
EndIf

Func AddStartup()
    RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO", "REG_SZ", @AutoItExe)
    MsgBox(0, "", "Added to startup.")
EndFunc

Func RemoveStartup()
    RegDelete("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO")
    MsgBox(0, "", "Deleted from startup and registry cleared.")
EndFunc

 

Link to comment
Share on other sites

Nothing below WEnd will run except for the AddStartup and RemoveStartup functions that are called from the GUI.  If the lines of the script between WEnd and the functions need to run before the GUI appears, just move the While 1...WEnd block of code before the functions.  If you want something to trigger those actions, wrap them in a function and call it from the GUI.

Link to comment
Share on other sites

You should move all the code before your While 1 loop otherwise is not going to run like @GMK said.
Also think about your run command because Run continue code execution even if the command you execute dont finish yet.

What I do when I need to use Run is to make a while loop right after the Run to check if the process finish and then continue with the rest of the code.
Example:
 

$cmd = Run(@Comspec & $comand, "", @SW_HIDE)
While ProcessExists($cmd)
    Sleep(100)
WEnd

Kind Regards
Alien.

Link to comment
Share on other sites

1 hour ago, alien4u said:

What I do when I need to use Run is to make a while loop right after the Run to check if the process finish and then continue with the rest of the code.

 

Or you could use RunWait.:)

Link to comment
Share on other sites

1 hour ago, RTFC said:

Or you could use RunWait.:)

Your are right but sometimes you need Run because you need $STDOUT_CHILD + $STDERR_CHILD.
Also in this case he must check stdout for errors.

Kind Regards
Alien.
 

Link to comment
Share on other sites

Your Button1 action only execute one of this functions base on a condition:

Func AddStartup()
    RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO", "REG_SZ", @AutoItExe)
    MsgBox(0, "", "Added to startup.")
EndFunc

Func RemoveStartup()
    RegDelete("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO")
    MsgBox(0, "", "Deleted from startup and registry cleared.")
EndFunc

What exactly is not working after you move the code?

Link to comment
Share on other sites

i moved it as below and caused button1 + gui_event_close didnt work. maybe it is because of my Do..Until loop ? if yes, is there any solution ?

#RequireAdmin

Global $file, $file2, $time, $exit, $second, $first
Global $NAC, $NA

;-------------------------------------------------------------------------------------------;

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPIDiag.au3>
#include <TrayConstants.au3>
#include <MsgBoxConstants.au3>

;--------------------Script After WhileEnd wont work if i use this GUI, anyone please help me ??---------------------------------
$Form1 = GUICreate("Form1", 387, 80, 188, 138)
$Label1 = GUICtrlCreateLabel("Default Gateway", 24, 16, 83, 17)
$Label2 = GUICtrlCreateLabel("MAC Address", 24, 48, 68, 17)
$Label3 = GUICtrlCreateLabel(":", 112, 16, 7, 17)
$Label4 = GUICtrlCreateLabel(":", 112, 48, 7, 17)
$Label5 = GUICtrlCreateLabel("", 136, 16, 132, 20)
$Label6 = GUICtrlCreateLabel("", 136, 48, 132, 20)
$button1 = GUICtrlCreateButton("Add To Startup", 288, 8, 81, 65)
GUISetState(@SW_SHOW)


if RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO") then
        GUICtrlSetData($button1,"Kill From Startup")
EndIf

;-----------------the below script wont work anymore if i use above GUI---------------------------------

$Services = ObjGet("winmgmts:\\.\root\CIMV2")
$NACs = $Services.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True')
For $NAC In $NACs
     $NAs = $Services.ExecQuery('SELECT * FROM Win32_NetworkAdapter')
     For $NA In $NAs
     If  $NA.MACAddress = $NAC.MACAddress  Then
        Exitloop 2
     EndIf
Next
Next



If _WinAPI_IsNetworkAlive() Then

    $comand=" /c arp "
    $comand=$comand&$NAC.DefaultIPGateway(0)&" -g>C:\ORIGINAL.txt"
    $cmd = Run(@Comspec & $comand, "", @SW_HIDE)

    $comand=" /c arp "
    $comand=$comand&$NAC.DefaultIPGateway(0)&" -g>C:\UPDATE.txt"

    DO
        $cmd = Run(@Comspec & $comand, "", @SW_HIDE)

        sleep ($time)

        $file=FileOpen("C:\Update.txt",0)
        $first=FileReadLine($file, 4)
        $first=StringTrimRight($first,15)
        $first=StringTrimLeft($first,24)
        $file=FileClose("C:\Update.txt")

        $file2=FileOpen("C:\ORIGINAL.txt",0)
        $second=FileReadLine($file2, 4)
        $second=StringTrimRight($second,15)
        $second=StringTrimLeft($second,24)
        $file2=FileClose("C:\ORIGINAL.txt")

        if $first=$second Then
            $time=5000
        Else
            MsgBox(0,"DETECTION", "YOUR MAC ADDRESS WAS CHANGED, TRYING TO RESOLVING")
            $comand=" /c netsh -c ""interface ipv4"" set neighbors "&$NA.NetConnectionID&" "&$NAC.DefaultIPGateway(0)&" "&$second&" "
            $cmd = Run(@Comspec & $comand, "", @SW_HIDE)
            sleep(5000)
            $comand=" /c arp "
            $comand=$comand&$NAC.DefaultIPGateway(0)&" -g>C:\UPDATE.txt"
        EndIf


    until $exit
Else
        TrayTip("","No Connection, unable to start this application",5)
        Exit
EndIf

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
            FileDelete("C:\ORIGINAL.txt")
            FileDelete("C:\update.txt")
            Exit
    Case $button1
        If RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO") = "" then
                GUICtrlSetData($button1,"Kill From Startup")
                AddStartup()
            Else
                RemoveStartup()
                GUICtrlSetData($button1,"Run On Startup")
        EndIf

    EndSwitch
WEnd

Func AddStartup()
    RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO", "REG_SZ", @AutoItExe)
    MsgBox(0, "", "Added to startup.")
EndFunc

Func RemoveStartup()
    RegDelete("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO")
    MsgBox(0, "", "Deleted from startup and registry cleared.")
EndFunc

 

Edited by mark2
Link to comment
Share on other sites

Try like this:
 

#RequireAdmin

Global $file, $file2, $time, $exit, $second, $first
Global $NAC, $NA

;-------------------------------------------------------------------------------------------;

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPIDiag.au3>
#include <TrayConstants.au3>
#include <MsgBoxConstants.au3>

;--------------------Script After WhileEnd wont work if i use this GUI, anyone please help me ??---------------------------------
$Form1 = GUICreate("Form1", 387, 80, 188, 138)
$Label1 = GUICtrlCreateLabel("Default Gateway", 24, 16, 83, 17)
$Label2 = GUICtrlCreateLabel("MAC Address", 24, 48, 68, 17)
$Label3 = GUICtrlCreateLabel(":", 112, 16, 7, 17)
$Label4 = GUICtrlCreateLabel(":", 112, 48, 7, 17)
$Label5 = GUICtrlCreateLabel("", 136, 16, 132, 20)
$Label6 = GUICtrlCreateLabel("", 136, 48, 132, 20)
$button1 = GUICtrlCreateButton("Add To Startup", 288, 8, 81, 65)
GUISetState(@SW_SHOW)


if RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO") then
        GUICtrlSetData($button1,"Kill From Startup")
EndIf



While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
            FileDelete("C:\ORIGINAL.txt")
            FileDelete("C:\update.txt")
            Exit
    Case $button1
        If RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO") = "" then
                GUICtrlSetData($button1,"Kill From Startup")
                AddStartup()
            Else
                RemoveStartup()
                GUICtrlSetData($button1,"Run On Startup")
        EndIf

    EndSwitch

;-----------------the below script wont work anymore if i use above GUI---------------------------------

$Services = ObjGet("winmgmts:\\.\root\CIMV2")
$NACs = $Services.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True')
For $NAC In $NACs
     $NAs = $Services.ExecQuery('SELECT * FROM Win32_NetworkAdapter')
     For $NA In $NAs
     If  $NA.MACAddress = $NAC.MACAddress  Then
        Exitloop 2
     EndIf
Next
Next



If _WinAPI_IsNetworkAlive() Then

    $comand=" /c arp "
    $comand=$comand&$NAC.DefaultIPGateway(0)&" -g>C:\ORIGINAL.txt"
    $cmd = Run(@Comspec & $comand, "", @SW_HIDE)

    $comand=" /c arp "
    $comand=$comand&$NAC.DefaultIPGateway(0)&" -g>C:\UPDATE.txt"

    $cmd = Run(@Comspec & $comand, "", @SW_HIDE)

        sleep ($time)

        $file=FileOpen("C:\Update.txt",0)
        $first=FileReadLine($file, 4)
        $first=StringTrimRight($first,15)
        $first=StringTrimLeft($first,24)
        $file=FileClose("C:\Update.txt")

        $file2=FileOpen("C:\ORIGINAL.txt",0)
        $second=FileReadLine($file2, 4)
        $second=StringTrimRight($second,15)
        $second=StringTrimLeft($second,24)
        $file2=FileClose("C:\ORIGINAL.txt")

        if $first=$second Then
            $time=5000
        Else
            MsgBox(0,"DETECTION", "YOUR MAC ADDRESS WAS CHANGED, TRYING TO RESOLVING")
            $comand=" /c netsh -c ""interface ipv4"" set neighbors "&$NA.NetConnectionID&" "&$NAC.DefaultIPGateway(0)&" "&$second&" "
            $cmd = Run(@Comspec & $comand, "", @SW_HIDE)
            sleep(5000)
            $comand=" /c arp "
            $comand=$comand&$NAC.DefaultIPGateway(0)&" -g>C:\UPDATE.txt"
        EndIf
Else
        TrayTip("","No Connection, unable to start this application",5)
        Exit
EndIf

WEnd

Func AddStartup()
    RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO", "REG_SZ", @AutoItExe)
    MsgBox(0, "", "Added to startup.")
EndFunc

Func RemoveStartup()
    RegDelete("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "AUTO")
    MsgBox(0, "", "Deleted from startup and registry cleared.")
EndFunc

You need to study Loops and how they work, if you want to make something until script exist and you see that GUI already have a While Loop why you need to create another loop? Use what you have and take advantage of that.

You will realize that some times you try to use to many for loops and in the end you will be fine with only one for loop and some conditional inside.

Happy Coding...

Kind Regards
Alien.

 

Link to comment
Share on other sites

i had tried it and it is also didnt work for the button1 and gui_event_close.....

i used the do...until loop as i would like to keep creating the second file only which is update.txt in order to check whether the contain of the first and second file still same or not.

Link to comment
Share on other sites

Im going to bed now but try to explain what you want to do or what are you doing, but not with auto it code, try to explain it with pseudo code, I mean in a logic way you want to do it.

So I can understand...

Kind Regards
Alien.

Link to comment
Share on other sites

what i want to do was work well if without GUI. but when i add GUI then GUI (button+gui_event_close) didnt work and some part of the code didnt work also.

if connection_is_ok then

    Create First File which contain Default Gateway and MAC Address.

    repeat (for create second file and read mac address of both files)
        Create Second File which also contain Default Gateway and MAC Address.

        read Mac Address of both files.

        if MAC Address of first file = MAC Address of second file then
            do nothing
        else
            set the arp to be static.
    until exit
    
else
exit

GUI just to provide information about
Default Gateway :
Mac Address of Router :
Button for execute addstartup and removestartup

 

Link to comment
Share on other sites

21 hours ago, alien4u said:

Your are right but sometimes you need Run because you need $STDOUT_CHILD + $STDERR_CHILD.
Also in this case he must check stdout for errors.

 

See ProgAndy's example code in post #2 of this thread.

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...