Jump to content

Recommended Posts

Posted

Hi,

The script bellow supposed to connect/disconnect my nic from the network per period of time.

I want using it for testing some network application but somehow it display Fatal Error.

I could not find were it comes from. Do you?

WARNNING: This application will disconnet your PC from the network.

please help.

#include <GUIConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <Timers.au3>
$hGUI = GUICreate("Net Bouncer", 278, 71, 193, 115)
$Start = GUICtrlCreateButton("Start", 8, 10, 75, 25, 0)
$Stop = GUICtrlCreateButton("Stop", 8, 35, 75, 25, 0)
$Interval = GUICtrlCreateInput("30000", 96, 8, 97, 21, $ES_NUMBER)
$Loops = GUICtrlCreateInput("2", 96, 35, 97, 21, $ES_NUMBER)
$Label1 = GUICtrlCreateLabel("Mili-Seconds", 200, 8, 76, 17, $SS_SUNKEN)
$Label2 = GUICtrlCreateLabel("Loops", 200, 35, 76, 17, $SS_SUNKEN)
GUISetState(@SW_SHOW)
Global $realTime, $iTimerProgress
GUICtrlSetState($Stop, $GUI_DISABLE)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            If GUICtrlGetState($Stop) = $GUI_ENABLE Then
                _Timer_KillAllTimers($hGUI)
            EndIf
            Exit
        Case $Stop
            GUICtrlSetState($Stop, $GUI_DISABLE)
            GUICtrlSetState($Start, $GUI_ENABLE)
            _Timer_KillTimer($hGUI, $iTimerProgress)
            
        Case $Start
            If Not StringStripWS(GUICtrlRead($Interval), 8) = "" Then
                GUICtrlSetState($Start, $GUI_DISABLE)
                GUICtrlSetState($Stop, $GUI_ENABLE)
                $realTime = Number(GUICtrlRead($Interval))
                If $realTime < 5000 Then
                    GUICtrlSetData($Interval, 5000)
                    $realTime = 5000
                EndIf
                $iTimerProgress = _Timer_SetTimer($hGUI, Number($realTime), "SwitchConnection")
            Else
                MsgBox(8208, "Error", "Please type interval in seconds")
            EndIf
    EndSwitch
WEnd

Func SwitchConnection()
    Local $StrCon = "Local Area Connection"
    $status = ''
    $status_ = ''
    $objwmiservice = ObjGet('winmgmts:\\localhost\root\CIMV2')
    $colitems = $objwmiservice.ExecQuery('SELECT * FROM Win32_NetworkAdapter', 'WQL', 0x10 + 0x20)
    If IsObj($colitems) Then
        For $objitem In $colitems
            If String($objitem.netconnectionid) = $StrCon Then
                $status = Not (Not ($objitem.netconnectionstatus))
                ExitLoop
            EndIf
        Next
    EndIf
    $shellapp = ObjCreate('shell.application')
    $ocontrolpanel = $shellapp.Namespace(3)
    For $folderitem In $ocontrolpanel.items
        If $folderitem.name = 'Network Connections' Then
            For $folderitem In $folderitem.getfolder.items
                If StringLower($folderitem.name) = StringLower($StrCon) Then
                    For $verb In $folderitem.verbs
                        If $verb.name = 'En&able' Or $verb.name = 'Disa&ble' Then
                            $verb.doit
                            $exitSwitchConnection = 0
                            While $exitSwitchConnection = 0
                                Sleep(1)
                                $colitems = $objwmiservice.ExecQuery('SELECT * FROM Win32_NetworkAdapter', 'WQL', 0x10 + 0x20)
                                If IsObj($colitems) Then
                                    For $objitem In $colitems
                                        If String($objitem.netconnectionid) = $StrCon Then $status_ = Not (Not ($objitem.netconnectionstatus))
                                        If $status = Not ($status_) Then
                                            $exitSwitchConnection = 1
                                            ExitLoop
                                        EndIf
                                    Next
                                EndIf
                            WEnd
                            Return 1
                        EndIf
                    Next
                EndIf
            Next
        EndIf
    Next
    
    Return 0
EndFunc  ;==>SwitchConnection

Be Green Now or Never (BGNN)!

Posted

what is timer.au3?

can you show me whats inside?

Timers.au3 comes with the Beta.

#include-once

; #INDEX# ====================================================================================================

===================
; Title .........: Timers
; AutoIt Version: 3.2.3++
; Language:    English
; Description ...: An application uses a timer to schedule an event for a window after a specified time has elapsed.
;                 Each time the specified interval (or time-out value) for a timer elapses, the system notifies the window
;                 associated with the timer. Because a timer's accuracy depends on the system clock rate and how often the
;                 application retrieves messages from the message queue, the time-out value is only approximate.
; Author ........: Gary Frost
; ====================================================================================================

===========================

; #VARIABLES# ====================================================================================================

===============
Global $_Timers_aTimerIDs[1][3]
; ====================================================================================================

===========================

; ====================================================================================================

===========================
; #CURRENT# ====================================================================================================

=================
;_Timer_Diff
;_Timer_GetTimerID
;_Timer_Init
;_Timer_KillAllTimers
;_Timer_KillTimer
;_Timer_SetTimer
; ====================================================================================================

===========================

; #INTERNAL_USE_ONLY#=================================================================================

===========================
;_Timer_QueryPerformanceCounter
;_Timer_QueryPerformanceFrequency
; ====================================================================================================

===========================

; #FUNCTION# ====================================================================================================

================
; Name...........: _Timer_Diff
; Description ...: Returns the difference in time from a previous call to _Timer_Init
; Syntax.........: _Timer_Diff($iTimeStamp)
; Parameters ....: $iTimeStamp - Timestamp returned from a previous call to _Timer_Init().
; Return values .: Success - Returns the time difference (in milliseconds) from a previous call to _Timer_Init().
; Author ........: Gary Frost, original by Toady
; Modified.......:
; Remarks .......:
; Related .......: _Timer_Diff
; Link ..........;
; Example .......; Yes
; ====================================================================================================

===========================
Func _Timer_Diff($iTimeStamp)
    Return 1000 * (_Timer_QueryPerformanceCounter() - $iTimeStamp) / _Timer_QueryPerformanceFrequency()
EndFunc  ;==>_Timer_Diff

; #FUNCTION# ====================================================================================================

================
; Name...........: _Timer_GetTimerID
; Description ...: Returns the Timer ID from $iwParam
; Syntax.........: _Timer_GetTimerID($iwParam)
; Parameters ....: $iwParam - Specifies the timer identifier event.
; Return values .: Success - The Timer ID
;                 Failure - 0
; Author ........: Gary Frost
; Modified.......:
; Remarks .......:
; Related .......: _Timer_SetTimer
; Link ..........;
; Example .......; Yes
; ====================================================================================================

===========================
Func _Timer_GetTimerID($iwParam)
    Local $_iTimerID = Dec(Hex($iwParam, 8)), $iMax = UBound($_Timers_aTimerIDs) - 1
    For $x = 1 To $iMax
        If $_iTimerID = $_Timers_aTimerIDs[$x][1] Then Return $_Timers_aTimerIDs[$x][0]
    Next
    Return 0
EndFunc  ;==>_Timer_GetTimerID

; #FUNCTION# ====================================================================================================

================
; Name...........: _Timer_Init
; Description ...: Returns a timestamp (in milliseconds).
; Syntax.........: _Timer_Init()
; Parameters ....:
; Return values .: Success - Returns a timestamp number (in milliseconds).
; Author ........: Gary Frost, original by Toady
; Modified.......:
; Remarks .......:
; Related .......: _Timer_Diff
; Link ..........;
; Example .......; Yes
; ====================================================================================================

===========================
Func _Timer_Init()
    Return _Timer_QueryPerformanceCounter()
EndFunc  ;==>_Timer_Init

; #FUNCTION# ====================================================================================================

================
; Name...........: _Timer_KillAllTimers
; Description ...: Destroys all the timers
; Syntax.........: _Timer_KillAllTimers($hWnd)
; Parameters ....: $hWnd        - Handle to the window associated with the timers.
;                 |This value must be the same as the hWnd value passed to the _Timer_SetTimer function that created the timer
; Return values .: Success - True
;                 Failure - False
; Author ........: Gary Frost
; Modified.......:
; Remarks .......: The _Timer_KillAllTimers function does not remove WM_TIMER messages already posted to the message queue
; Related .......: _Timer_KillTimer, _Timer_SetTimer
; Link ..........;
; Example .......; Yes
; ====================================================================================================

===========================
Func _Timer_KillAllTimers($hWnd)
    Local $iResult, $hCallBack = 0
    For $x = UBound($_Timers_aTimerIDs) - 1 To 1 Step -1
        If Not IsHWnd($hWnd) Then
            $iResult = DllCall("user32.dll", "int", "KillTimer", "hwnd", $hWnd, "int", $_Timers_aTimerIDs[$x][1])
            If @error Then Return SetError(-1, -1, False)
        Else
            $iResult = DllCall("user32.dll", "int", "KillTimer", "hwnd", $hWnd, "int", $_Timers_aTimerIDs[$x][0])
            If @error Then Return SetError(-1, -1, False)
        EndIf
        $hCallBack = $_Timers_aTimerIDs[$x][2]
        If $hCallBack <> 0 Then DllCallbackFree($hCallBack)
        ReDim $_Timers_aTimerIDs[UBound($_Timers_aTimerIDs)][3]
        $_Timers_aTimerIDs[0][0] -= 1
    Next
    Return $iResult <> 0
EndFunc  ;==>_Timer_KillAllTimers

; #FUNCTION# ====================================================================================================

================
; Name...........: _Timer_KillTimer
; Description ...: Destroys the specified timer
; Syntax.........: _Timer_KillTimer($hWnd, $iTimerID)
; Parameters ....: $hWnd        - Handle to the window associated with the specified timer.
;                 |This value must be the same as the hWnd value passed to the _Timer_SetTimer function that created the timer
;                 $iTimerID   - Specifies the timer to be destroyed
; Return values .: Success - True
;                 Failure - False
; Author ........: Gary Frost
; Modified.......:
; Remarks .......: The _Timer_KillTimer function does not remove WM_TIMER messages already posted to the message queue
; Related .......: _Timer_KillAllTimers, _Timer_SetTimer
; Link ..........; @@MsdnLink@@ KillTimer
; Example .......; Yes
; ====================================================================================================

===========================
Func _Timer_KillTimer($hWnd, $iTimerID)
    Local $iResult, $hCallBack = 0
    For $x = 1 To UBound($_Timers_aTimerIDs) - 1
        If $_Timers_aTimerIDs[$x][0] = $iTimerID Then
            If Not IsHWnd($hWnd) Then
                $iResult = DllCall("user32.dll", "int", "KillTimer", "hwnd", $hWnd, "int", $_Timers_aTimerIDs[$x][1])
            Else
                $iResult = DllCall("user32.dll", "int", "KillTimer", "hwnd", $hWnd, "int", $_Timers_aTimerIDs[$x][0])
            EndIf
            If @error Then Return SetError(-1, -1, False)
            $hCallBack = $_Timers_aTimerIDs[$x][2]
            If $hCallBack <> 0 Then DllCallbackFree($hCallBack)
            ReDim $_Timers_aTimerIDs[UBound($_Timers_aTimerIDs)][3]
            $_Timers_aTimerIDs[0][0] -= 1
            ExitLoop
        EndIf
    Next
    Return $iResult[0] <> 0
EndFunc  ;==>_Timer_KillTimer

; #INTERNAL_USE_ONLY#=================================================================================

===========================
; Name...........: _Timer_QueryPerformanceCounter
; Description ...: Retrieves the current value of the high-resolution performance counter
; Syntax.........: _Timer_QueryPerformanceCounter()
; Parameters ....:
; Return values .: Success - Current performance-counter value, in counts
;                 Failure - -1
; Author ........: Gary Frost
; Modified.......:
; Remarks .......:
; Related .......: _Timer_QueryPerformanceFrequency
; Link ..........; @@MsdnLink@@ QueryPerformanceCounter
; Example .......;
; ====================================================================================================

===========================
Func _Timer_QueryPerformanceCounter()
    Local $tperf = DllStructCreate("int64")
    DllCall("kernel32.dll", "int", "QueryPerformanceCounter", "ptr", DllStructGetPtr($tperf))
    If @error Then Return SetError(-1, -1, -1)
    Return DllStructGetData($tperf, 1)
EndFunc  ;==>_Timer_QueryPerformanceCounter

; #INTERNAL_USE_ONLY#=================================================================================

===========================
; Name...........: _Timer_QueryPerformanceFrequency
; Description ...: Retrieves the current value of the high-resolution performance counter
; Syntax.........: _Timer_QueryPerformanceFrequency()
; Parameters ....:
; Return values .: Success - Current performance-counter frequency, in counts per second
;                 Failure - 0
; Author ........: Gary Frost
; Modified.......:
; Remarks .......: If the installed hardware does not support a high-resolution performance counter, the return can be zero.
; Related .......: _Timer_QueryPerformanceCounter
; Link ..........; @@MsdnLink@@ QueryPerformanceCounter
; Example .......;
; ====================================================================================================

===========================
Func _Timer_QueryPerformanceFrequency()
    Local $tperf = DllStructCreate("int64")
    DllCall("kernel32.dll", "int", "QueryPerformanceFrequency", "ptr", DllStructGetPtr($tperf))
    If @error Then Return SetError(-1, -1, 0)
    Return DllStructGetData($tperf, 1)
EndFunc  ;==>_Timer_QueryPerformanceFrequency

; #FUNCTION# ====================================================================================================

================
; Name...........: _Timer_SetTimer
; Description ...: Creates a timer with the specified time-out value
; Syntax.........: _Timer_SetTimer($hWnd[, $iElapse = 250[, $sTimerFunc = ""[, $iTimerID = -1]]])
; Parameters ....: $hWnd        - Handle to the window to be associated with the timer.
;                 |This window must be owned by the calling thread
;                 $iElapse   - Specifies the time-out value, in milliseconds
;                 $sTimerFunc  - Function name to be notified when the time-out value elapses
;                 $iTimerID - Specifies a timer identifier.
;                 |If $iTimerID = -1 then a new timer is created
;                 |If $iTimerID matches an existing timer then the timer is replaced
;                 |If $iTimerID = -1 and $sTimerFunc = "" then timer will use WM_TIMER events
; Return values .: Success - Integer identifying the new timer
;                 Failure - 0
; Author ........: Gary Frost
; Modified.......:
; Remarks .......:
; Related .......: _Timer_KillTimer, _Timer_KillAllTimers, _Timer_GetTimerID
; Link ..........; @@MsdnLink@@ SetTimer
; Example .......; Yes
; ====================================================================================================

===========================
Func _Timer_SetTimer($hWnd, $iElapse = 250, $sTimerFunc = "", $iTimerID = -1)
    Local $iResult[1], $pTimerFunc = 0, $hCallBack = 0, $iIndex = $_Timers_aTimerIDs[0][0] + 1
    If $iTimerID = -1 Then; create a new timer
        ReDim $_Timers_aTimerIDs[$iIndex + 1][3]
        $_Timers_aTimerIDs[0][0] = $iIndex
        $iTimerID = $iIndex + 1000
        For $x = 1 To $iIndex
            If $_Timers_aTimerIDs[$x][0] = $iTimerID Then
                $iTimerID = $iTimerID + 1
                $x = 0
            EndIf
        Next
        If $sTimerFunc <> "" Then; using callbacks, if $sTimerFunc = "" then using WM_TIMER events
            $hCallBack = DllCallbackRegister($sTimerFunc, "none", "hwnd;int;int;dword")
            If $hCallBack = 0 Then Return SetError(-1, -1, 0)
            $pTimerFunc = DllCallbackGetPtr($hCallBack)
            If $pTimerFunc = 0 Then Return SetError(-1, -1, 0)
        EndIf
        $iResult = DllCall("user32.dll", "int", "SetTimer", "hwnd", $hWnd, "int", $iTimerID, "int", $iElapse, "ptr", $pTimerFunc)
        If @error Then Return SetError(-1, -1, 0)
        $_Timers_aTimerIDs[$iIndex][0] = $iResult[0]; integer identifier
        $_Timers_aTimerIDs[$iIndex][1] = $iTimerID; timer id
        $_Timers_aTimerIDs[$iIndex][2] = $hCallBack; callback identifier, need this for the Kill Timer
    Else; reuse timer
        For $x = 1 To $iIndex - 1
            If $_Timers_aTimerIDs[$x][0] = $iTimerID Then
                $iTimerID = $_Timers_aTimerIDs[$x][1]
                $hCallBack = $_Timers_aTimerIDs[$x][2]
                If $hCallBack <> 0 Then; call back was used to create the timer
                    $pTimerFunc = DllCallbackGetPtr($hCallBack)
                    If $pTimerFunc = 0 Then Return SetError(-1, -1, 0)
                EndIf
                $iResult = DllCall("user32.dll", "int", "SetTimer", "hwnd", $hWnd, "int", $iTimerID, "int", $iElapse, "ptr", $pTimerFunc)
                If @error Then Return SetError(-1, -1, 0)
                ExitLoop
            EndIf
        Next
    EndIf
    Return $iResult[0]
EndFunc  ;==>_Timer_SetTimer

Be Green Now or Never (BGNN)!

Posted (edited)

What error, specificly?

the error is.

AVector : []: out of bounds

To reproduce: click on the start button.

I simplified the script. The error came probably because of the "_Timer_SetTimer" function!!!

And this happens when calling the function with a function to be executed only!

maybe it is specific to my environment?

#include <GUIConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <Timers.au3>
$hGUI = GUICreate("Net Bouncer", 278, 71, 193, 115)
$Start = GUICtrlCreateButton("Start", 8, 10, 75, 25, 0)
$Stop = GUICtrlCreateButton("Stop", 8, 35, 75, 25, 0)
$Interval = GUICtrlCreateInput("30000", 96, 8, 97, 21, $ES_NUMBER)
$Loops = GUICtrlCreateInput("2", 96, 35, 97, 21, $ES_NUMBER)
$Label1 = GUICtrlCreateLabel("Mili-Seconds", 200, 8, 76, 17, $SS_SUNKEN)
$Label2 = GUICtrlCreateLabel("Loops", 200, 35, 76, 17, $SS_SUNKEN)
GUISetState(@SW_SHOW)
Global $realTime, $iTimerProgress
GUICtrlSetState($Stop, $GUI_DISABLE)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            If GUICtrlGetState($Stop) = $GUI_ENABLE Then
                _Timer_KillAllTimers($hGUI)
            EndIf
            Exit
        Case $Stop
            GUICtrlSetState($Stop, $GUI_DISABLE)
            GUICtrlSetState($Start, $GUI_ENABLE)
            _Timer_KillTimer($hGUI, $iTimerProgress)
            
        Case $Start
           
                $iTimerProgress = _Timer_SetTimer($hGUI, 1000, "SwitchConnection")
           
    EndSwitch
WEnd

Func SwitchConnection()
    consolewrite("blalala")
EndFunc
Edited by lsakizada

Be Green Now or Never (BGNN)!

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
  • Recently Browsing   0 members

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