gcue

function unexpectedly running multiple times

6 posts in this topic

#1 ·  Posted (edited)

hello

i am trying to enable/disable the wireless network adaptors under these conditions:

when wired connection is active disable wireless

when wired connection is NOT active enable wireless

it works well except i keep getting the tray icon popup... probably because its running the function again and again... but i thought my IF THEN statements in the while loop were enough to only run it once.

can anyone take a gander?

thank you in advance!!!

#RequireAdmin
#include <Array.au3>

Opt("GUIOnEventMode", 1)

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1 + 2)
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "_Exit")

TraySetToolTip(StringReplace(@ScriptName, ".exe", ""))

TraySetState(1)

Global $script_name = "Toggle Wifi When On LAN", $script_exe = $script_name & ".exe"
Global $script_version = $script_name & " v" & FileGetVersion(@ScriptName, "FileVersion")
Global $msg_normal = 262144, $msg_prompt = 262148, $msg_retry = 262150, $msg_error = 262160

Global $script_dir = @AppDataDir & "\" & $script_name
Global $wired_connected, $wireless_connected

FilesAdd()

While 1
    NetworkTest()

    If $wired_connected = True Then
        If $wireless_connected = True Then WirelessAdaptor("Disable")
    EndIf

    If $wired_connected = False Then
        If $wireless_connected = False Then WirelessAdaptor("Enable")
    EndIf

    Sleep(5000)
WEnd

Func NetworkTest()

    ;NetConnectionStatus
    ;0-"Disconnected"
    ;1-"Connecting"
    ;2-"Connected"
    ;3-"Disconnecting"
    ;4-"Hardware not present"
    ;5-"Hardware disabled"
    ;6-"Hardware malfunction"
    ;7-"Media disconnected"
    ;8-"Authenticating"
    ;9-"Authentication succeeded"
    ;10-"Authentication failed"
    ;11-"Invalid address"
    ;12-"Credentials required"

    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""

    $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID='Local Area Connection' OR NetConnectionID='Wireless Network Connection'", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            $name = $objItem.Name
            $type = $objItem.NetConnectionID
            $status = $objItem.NetConnectionStatus

            If $type = "Local Area Connection" Then
                If $status = 2 Then
                    $wired_connected = True ;2
                Else
                    $wired_connected = False ;7
                EndIf
            EndIf

            If $type = "Wireless Network Connection" Then
                If $status = 2 Then
                    $wireless_connected = True ;2
                Else
                    $wireless_connected = False ;0
                EndIf
            EndIf
        Next
    Else
        MsgBox($msg_error, $script_name, "No WMI Objects Found for class: " & "Win32_NetworkAdapter")
    EndIf

EndFunc   ;==>NetworkTest

Func WirelessAdaptor($action)

    TrayTip("Wireless Network Adaptor", $action & "d", 3, 16)
    TraySetToolTip($script_name & @CRLF & @CRLF & "Wireless " & $action & "d")

    If $action = "Enable" Then TraySetIcon($script_dir & "\twwol.dll", 1)
    If $action = "Disable" Then TraySetIcon($script_dir & "\twwol.dll", 2)

    RunWait(@ComSpec & ' /c netsh interface set interface name="Wireless Network Connection" ' & $action, "", @SW_HIDE)

EndFunc   ;==>WirelessAdaptor

Func FilesAdd()

    If Not FileExists($script_dir) Then
        DirCreate($script_dir)
    EndIf

    FileInstall("D:\Users\Administrator\Desktop\toggle\twwol.dll", $script_dir & "\twwol.dll", 1)

EndFunc   ;==>FilesAdd

Func FilesDelete()
    FileDelete($script_dir & "\twwol.dll")
EndFunc   ;==>FilesDelete

Func Debug($variable1 = "", $variable2 = "", $variable3 = "", $variable4 = "")

;~  #include <array.au3>
;~  $msg_normal = 0

    If IsArray($variable1) Then
        _ArrayDisplay($variable1)
    Else
        If $variable2 <> "" Then
            $variable1 &= @CRLF & $variable2
        EndIf

        If $variable3 <> "" Then
            $variable1 &= @CRLF & $variable3
        EndIf

        If $variable4 <> "" Then
            $variable1 &= @CRLF & $variable4
        EndIf

        ClipPut($variable1)
        MsgBox($msg_normal, "Debug", $variable1)
    EndIf

EndFunc   ;==>Debug

Func _Exit()
    FilesDelete()
    Exit
EndFunc   ;==>_Exit

 

Edited by gcue

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hey man.  Let me do you a favor and save you some time with a solution that already does that.  Or at the very least you can use it as a reference.

Edited by MattHiggs

Share this post


Link to post
Share on other sites

very cool tool.  thanks for that.. i am still curious however why its not working as intended.

 

Share this post


Link to post
Share on other sites
2 minutes ago, gcue said:

very cool tool.  thanks for that.. i am still curious however why its not working as intended.

 

Hmmm.  Not sure why it continously keeps restarting, but I do know that, using wmi explorer, I could not find an entry for "Win32_NetworkAdapter" under "root\CIMv2".  You might want to verify the WMI setup on the machine using aforementioned tool.

Share this post


Link to post
Share on other sites

@gcue The reason I believe it keeps firing is that the variables $wired_connected and $wireless_connected remain the same.  At the bottom of your NetworkTest() function if you put a MsgBox displaying $wired_connected and $wireless_connected, what does it return?

Nb: I would have personally placed AdLibRegister before the loop and moved your check into the NetworkTest function.

 

 

Share this post


Link to post
Share on other sites

good catch.

i made the changes you suggested

thank you very much for your help!

hope you have a great weekend :)

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