Jump to content

[Solved] Script is forgetting the value of a variable when calling a function


Recommended Posts

I am calling an error checking function I made and it works fine except for line 110, where it seems to forget the value of $sCheckEnabled. The value for this variable is set before calling and after returning the function and the scope is set to Global. Additionally, I don't have this problem when I call it with any other variable. What am I doing wrong?

#include <AutoItConstants.au3>
#include <Array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $sOutput, $bWiFiState, $sSSID
Switch StringRight(@OSLang, 2)
    Case "09"
        Global $sWinTitle = "WiFi-Hotspot"
        Global $sStartCommand = " /c netsh wlan start hostednetwork"
        Global $sStopCommand = " /c netsh wlan stop hostednetwork"
        Global $sShowSettingsCommand = " /c netsh wlan show hostednetwork"
        Global $sChangeSSIDCommand = " /c netsh wlan set hostednetwork ssid="
        Global $sChangePassCommand = " /c netsh wlan set hostednetwork key="
        Global $sEnableHostedNetworkCommand = "/c netsh wlan set hostednetwork mode=allow"
        Global $sCheckStart = "The hosted network started."
        Global $sCheckStop = "The hosted network stopped."
        Global $sCheckEnabled = "The hosted network mode has been set to allow."
        Global $sCheckSSID = "The SSID of the hosted network has been successfully changed."
        Global $sCheckPass = "The user key passphrase of the hosted network has been successfully changed."
        Global $sErrorStart = "An error occured trying to "
        Global $sErrorEnd = " the WiFi-Hotspot."
        Global $sStart = "Start"
        Global $sStop = "Stop"
        Global $sGet = "get the current settings of"
        Global $sEnableHostedNetwork = "enable"
        Global $sEnableHostedNetworkExtended = " Your WiFi may not support this feature."
        Global $sStatus = "Status : "
        Global $sIsStarted = "Started"
        Global $sIsStopped = "Not started"
        Global $sConfigure = "Configure"
        Global $sExit = "Exit"
        Global $sSSIDLen = "Wifi Name must be between 1 and 32 character long."
        Global $sPassLen = "Password must be between 8 and 63 character long."
        Global $sApplied = "Your settings have been saved. You will need to restart the WiFi Hotspot to use the new settings."
    Case Else
        MsgBox($MB_ICONINFORMATION, "WiFi-Hotspot", "This software will only run properly if you language is English - United States. Please ask how you can provide translations at BetaLeaf@gmail.com")
EndSwitch

_GetSettings()

#Region ### START Koda GUI section ### Form=
$hWinTitle = GUICreate($sWinTitle, 130, 109, -1, -1, $WS_CAPTION)
If $bWiFiState = True Then
    $hWiFiState = GUICtrlCreateLabel($sStatus & $sIsStarted, 0, 0, 130)
Else
    $hWiFiState = GUICtrlCreateLabel($sStatus & $sIsStopped, 0, 0, 130)
EndIf
$hStart = GUICtrlCreateButton($sStart, 0, 12, 130, 25)
$hStop = GUICtrlCreateButton($sStop, 0, 36, 130, 25)
$hConfigure = GUICtrlCreateButton($sConfigure, 0, 60, 130, 25)
$hExit = GUICtrlCreateButton($sExit, 0, 84, 130, 25)
GUISetState(@SW_SHOW, $hWinTitle)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hStart
            GUICtrlSetState($hStart, $GUI_DISABLE)
            $pid = Run(@ComSpec & $sStartCommand, "", @SW_HIDE, $STDOUT_CHILD)
            If _Error($pid, $sCheckStart) = True Then
                MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sStart & $sErrorEnd)
            Else
                GUICtrlSetData($hWiFiState, $sStatus & $sIsStarted)
            EndIf
            GUICtrlSetState($hStart, $GUI_ENABLE)
        Case $hStop
            GUICtrlSetState($hStop, $GUI_DISABLE)
            $pid = Run(@ComSpec & $sStopCommand, "", @SW_HIDE, $STDOUT_CHILD)
            If _Error($pid, $sCheckStop) = True Then
                MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sStop & $sErrorEnd)
            Else
                GUICtrlSetData($hWiFiState, $sStatus & $sIsStopped)
            EndIf
            GUICtrlSetState($hStop, $GUI_ENABLE)
        Case $hConfigure

            #Region ### START Koda GUI section ### Form=
            $hConfig = GUICreate($sWinTitle & " - " & $sConfigure, 428, 84, -1, -1)
            $hSSID = GUICtrlCreateLabel("WiFi Name", 4, 8, 56, 17)
            $sInputSSID = GUICtrlCreateInput($sSSID, 64, 4, 360, 21)
            GUICtrlSetLimit($sInputSSID, 32)
            $hPass = GUICtrlCreateLabel("Password", 4, 32, 50, 17)
            $sInputPass = GUICtrlCreateInput("", 64, 30, 360, 21, $ES_PASSWORD)
            GUICtrlSetLimit($sInputSSID, 63)
            $hOk = GUICtrlCreateButton("Ok", 4, 56, 420, 25)
            GUISetState(@SW_SHOW, $hConfig)
            #EndRegion ### END Koda GUI section ###

            While 1
                $nMsg = GUIGetMsg()
                Switch $nMsg
                    Case $GUI_EVENT_CLOSE
                        GUIDelete($hConfig)
                        ExitLoop
                    Case $hOk
                        $sISSID = GUICtrlRead($sInputSSID)
                        $sIPass = GUICtrlRead($sInputPass)
                        Select
                            Case StringLen($sISSID) < 1
                                MsgBox($MB_ICONWARNING, $sWinTitle, $sSSIDLen)
                            Case StringLen($sIPass) < 1
                                MsgBox($MB_ICONWARNING, $sWinTitle, $sPassLen)
                            Case Else
                                $pid = Run(@ComSpec & $sEnableHostedNetworkCommand, "", @SW_HIDE, $STDOUT_CHILD)
                                If _Error($pid, $sCheckEnabled) = True Then ;ERROR HERE
                                    MsgBox(16, $sWinTitle, $sErrorStart & $sEnableHostedNetwork & $sErrorEnd & $sEnableHostedNetworkExtended)
                                    ContinueCase
                                EndIf
                                $pid = Run(@ComSpec & $sChangeSSIDCommand & '"' & $sISSID & '"', "", @SW_HIDE, $STDOUT_CHILD)
                                If _Error($pid, $sCheckSSID) = True Then
                                    MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sConfigure & $sErrorEnd)
                                    ContinueCase
                                Else
                                    $sSSID = $sISSID
                                EndIf
                                $pid = Run(@ComSpec & $sChangePassCommand & '"' & $sIPass & '"', "", @SW_HIDE, $STDOUT_CHILD)
                                If _Error($pid, $sCheckPass) = True Then
                                    MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sConfigure & $sErrorEnd)
                                    ContinueCase
                                Else
                                    MsgBox($MB_ICONERROR, $sWinTitle, $sApplied)
                                    GUIDelete($hConfig)
                                    ExitLoop
                                EndIf
                        EndSelect
                EndSwitch
            WEnd
        Case $hExit
            Exit
    EndSwitch
WEnd

Func _Error(ByRef $pid, $sExpectedOutput = "")
    ProcessWaitClose($pid)
    $sOutput = StdoutRead($pid)
    ConsoleWrite("Expecting:" & @CRLF & $sOutput & @CRLF & "Received:" & @CRLF & $sExpectedOutput & @CRLF & @CRLF)
    $iRet = StringInStr($sOutput, $sExpectedOutput)
    If $sExpectedOutput <> "" And $iRet = 0 Then
        Return True
    Else
        Return False
    EndIf
EndFunc   ;==>_Error

Func _GetSettings()
    $pid = Run(@ComSpec & $sShowSettingsCommand, "", @SW_HIDE, $STDOUT_CHILD)
    If _Error($pid) = True Then
        MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sGet & $sErrorEnd)
    Else
        $sSettings = StringStripWS($sOutput, 4)
        $aSettings = StringSplit(StringStripWS($sOutput, 4), @CRLF)
        Select
            Case StringInStr($aSettings[7], $sIsStarted, 1) > 0
                $bWiFiState = True
            Case StringInStr($aSettings[7], $sIsStopped, 1) > 0
                $bWiFiState = False
            Case @error
                MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sGet & $sErrorEnd)
            Case Else
                MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sGet & $sErrorEnd)
        EndSelect
        $sRet = StringReplace($aSettings[3], '"', "")
        $aRet = StringSplit($sRet, ":", 2)
        $sSSID = StringTrimLeft($aRet[1], 1)
    EndIf
EndFunc   ;==>_GetSettings

Edit: This issue was solved thanks to @Jos. Reveal the spoiler below for the completed code.

Spoiler
#include <AutoItConstants.au3>
#include <Array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $sOutput, $bWiFiState, $sSSID
Switch StringRight(@OSLang, 2)
    Case "09"
        Global $sWinTitle = "WiFi-Hotspot"
        Global $sStartCommand = " /c netsh wlan start hostednetwork"
        Global $sStopCommand = " /c netsh wlan stop hostednetwork"
        Global $sShowSettingsCommand = " /c netsh wlan show hostednetwork"
        Global $sChangeSSIDCommand = " /c netsh wlan set hostednetwork ssid="
        Global $sChangePassCommand = " /c netsh wlan set hostednetwork key="
        Global $sEnableHostedNetworkCommand = " /c netsh wlan set hostednetwork mode=allow"
        Global $sCheckStart = "The hosted network started."
        Global $sCheckStop = "The hosted network stopped."
        Global $sCheckEnabled = "The hosted network mode has been set to allow."
        Global $sCheckSSID = "The SSID of the hosted network has been successfully changed."
        Global $sCheckPass = "The user key passphrase of the hosted network has been successfully changed."
        Global $sErrorStart = "An error occured trying to "
        Global $sErrorEnd = " the WiFi-Hotspot."
        Global $sStart = "Start"
        Global $sStop = "Stop"
        Global $sGet = "get the current settings of"
        Global $sEnableHostedNetwork = "enable"
        Global $sEnableHostedNetworkExtended = " Your WiFi may not support this feature."
        Global $sStatus = "Status : "
        Global $sIsStarted = "Started"
        Global $sIsStopped = "Not started"
        Global $sConfigure = "Configure"
        Global $sExit = "Exit"
        Global $sSSIDLen = "Wifi Name must be between 1 and 32 character long."
        Global $sPassLen = "Password must be between 8 and 63 character long."
        Global $sApplied = "Your settings have been saved. You will need to restart the WiFi Hotspot to use the new settings."
    Case Else
        MsgBox($MB_ICONINFORMATION, "WiFi-Hotspot", "This software will only run properly if you language is English - United States. Please ask how you can provide translations at BetaLeaf@gmail.com")
EndSwitch
_GetSettings()

#Region ### START Koda GUI section ### Form=
$hWinTitle = GUICreate($sWinTitle, 130, 109, -1, -1, $WS_CAPTION)
If $bWiFiState = True Then
    $hWiFiState = GUICtrlCreateLabel($sStatus & $sIsStarted, 0, 0, 130)
Else
    $hWiFiState = GUICtrlCreateLabel($sStatus & $sIsStopped, 0, 0, 130)
EndIf
$hStart = GUICtrlCreateButton($sStart, 0, 12, 130, 25)
$hStop = GUICtrlCreateButton($sStop, 0, 36, 130, 25)
$hConfigure = GUICtrlCreateButton($sConfigure, 0, 60, 130, 25)
$hExit = GUICtrlCreateButton($sExit, 0, 84, 130, 25)
GUISetState(@SW_SHOW, $hWinTitle)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hStart
            GUICtrlSetState($hStart, $GUI_DISABLE)
            $pid = Run(@ComSpec & $sStartCommand, "", @SW_HIDE, $STDOUT_CHILD)
            If _Error($pid, $sCheckStart) = True Then
                MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sStart & $sErrorEnd)
            Else
                GUICtrlSetData($hWiFiState, $sStatus & $sIsStarted)
            EndIf
            GUICtrlSetState($hStart, $GUI_ENABLE)
        Case $hStop
            GUICtrlSetState($hStop, $GUI_DISABLE)
            $pid = Run(@ComSpec & $sStopCommand, "", @SW_HIDE, $STDOUT_CHILD)
            If _Error($pid, $sCheckStop) = True Then
                MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sStop & $sErrorEnd)
            Else
                GUICtrlSetData($hWiFiState, $sStatus & $sIsStopped)
            EndIf
            GUICtrlSetState($hStop, $GUI_ENABLE)
        Case $hConfigure

            #Region ### START Koda GUI section ### Form=
            $hConfig = GUICreate($sWinTitle & " - " & $sConfigure, 428, 84, -1, -1)
            $hSSID = GUICtrlCreateLabel("WiFi Name", 4, 8, 56, 17)
            $sInputSSID = GUICtrlCreateInput($sSSID, 64, 4, 360, 21)
            GUICtrlSetLimit($sInputSSID, 32)
            $hPass = GUICtrlCreateLabel("Password", 4, 32, 50, 17)
            $sInputPass = GUICtrlCreateInput("", 64, 30, 360, 21, $ES_PASSWORD)
            GUICtrlSetLimit($sInputSSID, 63)
            $hOk = GUICtrlCreateButton("Ok", 4, 56, 420, 25)
            GUISetState(@SW_SHOW, $hConfig)
            #EndRegion ### END Koda GUI section ###

            While 1
                $nMsg = GUIGetMsg()
                Switch $nMsg
                    Case $GUI_EVENT_CLOSE
                        GUIDelete($hConfig)
                        ExitLoop
                    Case $hOk
                        $sISSID = GUICtrlRead($sInputSSID)
                        $sIPass = GUICtrlRead($sInputPass)
                        Select
                            Case StringLen($sISSID) < 1
                                MsgBox($MB_ICONWARNING, $sWinTitle, $sSSIDLen)
                            Case StringLen($sIPass) < 1
                                MsgBox($MB_ICONWARNING, $sWinTitle, $sPassLen)
                            Case Else
                                GUICtrlSetState($hOk, $GUI_DISABLE)
                                $pid = Run(@ComSpec & $sEnableHostedNetworkCommand, "", @SW_HIDE, $STDOUT_CHILD)
                                If _Error($pid, $sCheckEnabled) = True Then
                                    MsgBox(16, $sWinTitle, $sErrorStart & $sEnableHostedNetwork & $sErrorEnd & $sEnableHostedNetworkExtended)
                                    ContinueCase
                                EndIf
                                $pid = Run(@ComSpec & $sChangeSSIDCommand & '"' & $sISSID & '"', "", @SW_HIDE, $STDOUT_CHILD)
                                If _Error($pid, $sCheckSSID) = True Then
                                    MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sConfigure & $sErrorEnd)
                                    ContinueCase
                                Else
                                    $sSSID = $sISSID
                                EndIf
                                $pid = Run(@ComSpec & $sChangePassCommand & '"' & $sIPass & '"', "", @SW_HIDE, $STDOUT_CHILD)
                                GUICtrlSetState($hOk, $GUI_ENABLE)
                                If _Error($pid, $sCheckPass) = True Then
                                    MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sConfigure & $sErrorEnd)
                                    ContinueCase
                                Else
                                    MsgBox($MB_ICONINFORMATION, $sWinTitle, $sApplied)
                                    GUIDelete($hConfig)
                                    ExitLoop
                                EndIf
                        EndSelect
                EndSwitch
            WEnd
        Case $hExit
            Exit
    EndSwitch
WEnd

Func _Error(ByRef $pid, $sExpectedOutput = "")
    ProcessWaitClose($pid)
    $sOutput = StdoutRead($pid)
    ConsoleWrite("Expecting:" & @CRLF & $sExpectedOutput & @CRLF & "Received:" & @CRLF & $sOutput & @CRLF & @CRLF)
    $iRet = StringInStr($sOutput, $sExpectedOutput)
    If $sExpectedOutput <> "" And $iRet = 0 Then
        Return True
    Else
        Return False
    EndIf
EndFunc   ;==>_Error

Func _GetSettings()
    $pid = Run(@ComSpec & $sShowSettingsCommand, "", @SW_HIDE, $STDOUT_CHILD)
    If _Error($pid) = True Then
        MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sGet & $sErrorEnd)
    Else
        $sSettings = StringStripWS($sOutput, 4)
        $aSettings = StringSplit(StringStripWS($sOutput, 4), @CRLF)
        Select
            Case StringInStr($aSettings[7], $sIsStarted, 1) > 0
                $bWiFiState = True
            Case StringInStr($aSettings[7], $sIsStopped, 1) > 0
                $bWiFiState = False
            Case @error
                MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sGet & $sErrorEnd)
            Case Else
                MsgBox($MB_ICONERROR, $sWinTitle, $sErrorStart & $sGet & $sErrorEnd)
        EndSelect
        $sRet = StringReplace($aSettings[3], '"', "")
        $aRet = StringSplit($sRet, ":", 2)
        $sSSID = StringTrimLeft($aRet[1], 1)
    EndIf
EndFunc   ;==>_GetSettings

 

 

Edited by BetaLeaf
Issue Solved

 

 

Link to comment
Share on other sites

As the variable gets declared in a Switch statement I would check if it is declared when the error function gets called.

MsgBox(0, "IsDeclared", IsDeclared($sCheckEnabled))


 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hmm. I'll try that when I get up tomorrow. Already headed to bed when I got the email. I tried seeing if it was set during the function with console write and it returned an empty string. I don't know why it would forget it got declared in global scope tho.

 

 

Link to comment
Share on other sites

6 hours ago, water said:

As the variable gets declared in a Switch statement I would check if it is declared when the error function gets called.

MsgBox(0, "IsDeclared", IsDeclared($sCheckEnabled))


 

This didn't work. When using the function isdeclared, you don't use the variable, you must use a string. I had to change your code to

MsgBox(0,"",IsDeclared("sCheckEnabled"))

This returned "1". This tells me that it is indeed declared in the global scope. I ran this line both inside and outside the error checking function and produced the same result. 

Additionally, I know the switch is loading my variables. Every variable in that script works except for $sCheckEnabled, despite being declared and defined.

Edited by BetaLeaf

 

 

Link to comment
Share on other sites

  • Developers
7 hours ago, BetaLeaf said:

    ConsoleWrite("Expecting:" & @CRLF & $sOutput & @CRLF & "Received:" & @CRLF & $sExpectedOutput & @CRLF & @CRLF)

You do know you have reversed the variables in this statement ...right? So isn't your $sOutput empty?

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

1 minute ago, Jos said:

You do know you have reversed the variables in this statement ...right? So isn't your $Output empty?

Jos

Sure enough, I missed that. With this new information, I have fixed the issue. In the variable $sEnableHostedNetworkCommand, I forgot a space before the switch /c. It was being used in conjunction with @comspec so to miss that space in a run function was causing the issue. Instead of "cmd.exe /c netsh wlan set hostednetwork mode=allow", with that missing space, it was actually "cmd.exe/c netsh wlan set hostednetwork mode=allow"

Thank you Jos.

 

 

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...