Jump to content

I need help turning my if statement into a function


gte
 Share

Recommended Posts

I have to either figure out how to turn this into a function, or have 31 if statements inside of my while statement :) .

My dilemma is this, I'm not sure how to have a function that can dynamically change the variable names inside of it OR maybe I'm thinking about it completely wrong, and I don't have to do that?

What I need to do is call this function 31 times inside of my while statement to monitor 31 different services, and then update a bunch of buttons and an inputbox and I'm not sure how to do that dynamically, as each button and inputbox has a different variable name? I've also copied in 2 separate boxes that reference 2 other functions that I'm calling inside of my if statements.

Thanks for reading.

if _ServiceRunning("Alerter") = 1   Then

    $bostonhostservicestartmode = ""
    $bostonhostservicestartstatus = ""
    _getbostonhostservicestate()
        if $bostonhostservicestartmode = 1 Then
            $bostonhostservicestartmode = "System"
            $bostonhostservicestartstatus = "Disable"
            
        elseif $bostonhostservicestartmode = 2 Then
            $bostonhostservicestartmode = "Automatic"
            $bostonhostservicestartstatus = "Disable"
                        
        ElseIf $bostonhostservicestartmode = 3 Then
            $bostonhostservicestartmode = "Manual"
            $bostonhostservicestartstatus = "Disable"
                        
        ElseIf $bostonhostservicestartmode = 4 Then
            $bostonhostservicestartmode = "Disabled"
            $bostonhostservicestartstatus = "Enable"
        
        Else
            $bostonhostservicestartmode = "Unknown"
            $bostonhostservicestartstatus = "Enable"
            GUICtrlSetState($bostonhoststartstop_button,$GUI_DISABLE)
            GUICtrlSetState($bostonhostenabledisable_button,$GUI_DISABLE)
            GUICtrlSetState($bostonhostrestart_button, $GUI_DISABLE)
        EndIf 
        
    GUICtrlSetData($bostonhost_input, "Running / " & $bostonhostservicestartmode)
    GUICtrlSetData($bostonhoststartstop_button, "Stop")
    GUICtrlSetData($bostonhostenabledisable_button, $bostonhostservicestartstatus)
    

    
ElseIf _ServiceRunning("Alerter") = 0   Then 

    $bostonhostservicestartmode = ""
    $bostonhostservicestartstatus = ""
    _getbostonhostservicestate()
        if $bostonhostservicestartmode = 1 Then
            $bostonhostservicestartmode = "System"
            $bostonhostservicestartstatus = "Disable"
            
        elseif $bostonhostservicestartmode = 2 Then
            $bostonhostservicestartmode = "Automatic"
            $bostonhostservicestartstatus = "Disable"
                        
        ElseIf $bostonhostservicestartmode = 3 Then
            $bostonhostservicestartmode = "Manual"
            $bostonhostservicestartstatus = "Disable"
                        
        ElseIf $bostonhostservicestartmode = 4 Then
            $bostonhostservicestartmode = "Disabled"
            $bostonhostservicestartstatus = "Enable"
        
        Else
            $bostonhostservicestartmode = "Unknown"
            $bostonhostservicestartstatus = "Enable"
            GUICtrlSetState($bostonhoststartstop_button,$GUI_DISABLE)
            GUICtrlSetState($bostonhostenabledisable_button,$GUI_DISABLE)
            GUICtrlSetState($bostonhostrestart_button, $GUI_DISABLE)
        EndIf
        
    GUICtrlSetData($bostonhost_input, "Stopped / " & $bostonhostservicestartmode)
    GUICtrlSetData($bostonhoststartstop_button, "Start")
    GUICtrlSetData($bostonhostenabledisable_button, $bostonhostservicestartstatus)

    
Else 
        $bostonhostservicestartmode = ""
        $bostonhostservicestartstatus = ""
    _getbostonhostservicestate()
        if $bostonhostservicestartmode = 1 Then
            $bostonhostservicestartmode = "System"
            
        elseif $bostonhostservicestartmode = 2 Then
            $bostonhostservicestartmode = "Automatic"
                        
        ElseIf $bostonhostservicestartmode = 3 Then
            $bostonhostservicestartmode = "Manual"
                        
        ElseIf $bostonhostservicestartmode = 4 Then
            $bostonhostservicestartmode = "Disabled"
        
        Else
            $bostonhostservicestartmode = "Unknown"
            $bostonhostservicestartstatus = "Enable"
            GUICtrlSetState($bostonhoststartstop_button,$GUI_DISABLE)
            GUICtrlSetState($bostonhostenabledisable_button,$GUI_DISABLE)
            GUICtrlSetState($bostonhostrestart_button, $GUI_DISABLE)
        EndIf
    $bostonhoststatus_button = "Error"
    GUICtrlSetData($bostonhoststartstop_button, "Error / " & $bostonhostservicestartmode)
    GUICtrlSetData($bostonhostenabledisable_button, $bostonhostservicestartstatus)

EndIf

func _getbostonhostservicestate()
    
    $bostonhostservicestartmode = RegRead("\\" & $servername &"\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\alerter\", "Start")
    
EndFunc

Func _ServiceRunning($sServiceName)
  Local $arRet
  Local $hSC
  Local $hService  
  Local $bRunning = 0

  $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
                   "str", "", _
                   "str", "ServicesActive", _
                   "long", $SC_MANAGER_CONNECT)
  If $arRet[0] <> 0 Then
     $hSC = $arRet[0]
     $arRet = DllCall("advapi32.dll", "long", "OpenService", _
                     "long", $hSC, _
                     "str", $sServiceName, _
                     "long", $SERVICE_INTERROGATE)
     If $arRet[0] <> 0 Then
        $hService = $arRet[0]
        $arRet = DllCall("advapi32.dll", "int", "ControlService", _
                         "long", $hService, _
                         "long", $SERVICE_CONTROL_INTERROGATE, _
                         "str", "")
        $bRunning = $arRet[0]
        DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
     EndIf
     DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
 EndIf

  Return $bRunning



EndFunc
Link to comment
Share on other sites

Really don't see how this can be improved? .....

Ok, you get one example! But after this you're going to have to try on your own, this is a big part of programming and I know a lot of people struggle with this (these people usually fail and give up on programming, but maybe you're different).

Here it is.. I think it looks fairly neat now, I hope it still works for you (you didn't give us much to work with).. This took me about 15 minutes to fix!

What you seem to lack is a rigid naming system. You should really, really work on that !!

While 1
    ; Get service state from the alerter service
    Local $serviceState = _ServiceRunning("Alerter")

    $modeAndStatus = _GetModeAndStatus()

    If $serviceState = 1 Then
        ; The service is running
        _UpdateGUI($modeAndStatus, 1)
    ElseIf $serviceState = 0 Then
        ; The service has stopped
        _UpdateGUI($modeAndStatus, 0)
    Else
        ; Status retreival errored
        _UpdateGUI($modeAndStatus, -1)
    EndIf

    Sleep(60000) ; Wait one minute
WEnd

; This function updates the GUI, it takes the service starting mode and a status for the service, and a number for the state of the service (stopped / running / error )
Func _UpdateGUI($modeAndStatus, $serviceState)
    Local $statusText = ""

    Switch $serviceState
        Case -1 ; Error
            $statusText = "Error"
        Case 0 ; Stopped
            $statusText = "Stopped"
            GUICtrlSetData($hoststartstop_button, "Start")
        Case 1 ; Running
            $statusText = "Running"
            GUICtrlSetData($hoststartstop_button, "Stop")
    EndSwitch

    GUICtrlSetData($host_input, $statusText " / " & _GetMode($modeAndStatus))
    GUICtrlSetData($hostenabledisable_button, _GetStatus($modeAndStatus))
EndFunc   ;==>_UpdateGUI

Func _GetMode($ar)
    Return $ar[0]
EndFunc   ;==>_GetMode

Func _GetStatus($ar)
    Return $ar[1]
EndFunc   ;==>_GetStatus

Func _GetModeAndStatus()
    Local $startMode = ""
    Local $startStatus = ""

    $startMode = _GetHostServiceState()

    Switch $startMode
        Case 1
            $startMode = "System"
            $startStatus = "Disable"
        Case 2
            $startMode = "Automatic"
            $startStatus = "Disable"
        Case 3
            $startMode = "Manual"
            $startStatus = "Disable"
        Case 4
            $startMode = "Disabled"
            $startStatus = "Enable"
        Case Else
            $startMode = "Unknown"
            $startStatus = "Enable"

            _DisableGUI()
    EndSwitch

    Local $return[2] = [$startMode, $startStatus]

    Return $return
EndFunc   ;==>_GetModeAndStatus

Func _DisableGUI()
    GUICtrlSetState($hoststartstop_button, $GUI_DISABLE)
    GUICtrlSetState($hostenabledisable_button, $GUI_DISABLE)
    GUICtrlSetState($hostRestart_Button, $GUI_DISABLE)
EndFunc   ;==>_DisableGUI

Func _GetHostServiceState()
    Return RegRead("\\" & $serverName & "\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\alerter\", "Start")
EndFunc   ;==>_GetHostServiceState

Func _ServiceRunning($sServiceName)
    Local $arRet
    Local $hSC
    Local $hService
    Local $bRunning = 0

    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
            "str", "", _
            "str", "ServicesActive", _
            "long", $SC_MANAGER_CONNECT)
    If $arRet[0] <> 0 Then
        $hSC = $arRet[0]
        $arRet = DllCall("advapi32.dll", "long", "OpenService", _
                "long", $hSC, _
                "str", $sServiceName, _
                "long", $SERVICE_INTERROGATE)
        If $arRet[0] <> 0 Then
            $hService = $arRet[0]
            $arRet = DllCall("advapi32.dll", "int", "ControlService", _
                    "long", $hService, _
                    "long", $SERVICE_CONTROL_INTERROGATE, _
                    "str", "")
            $bRunning = $arRet[0]
            DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
        EndIf
        DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf

    Return $bRunning
EndFunc   ;==>_ServiceRunning
Edited by Manadar
Link to comment
Share on other sites

Look at Select/Case/EndSelect

I'm using select and case in the while statements, for when I push different buttons already :)

Really don't see how this can be improved? .....

Ok, you get one example! But after this you're going to have to try on your own, this is a big part of programming and I know a lot of people struggle with this (these people usually fail and give up on programming, but maybe you're different).

Here it is.. I think it looks fairly neat now, I hope it still works for you (you didn't give us much to work with).. This took me about 15 minutes to fix!

What you seem to lack is a rigid naming system. You should really, really work on that !!

While 1
    ; Get service state from the alerter service
    Local $serviceState = _ServiceRunning("Alerter")

    $modeAndStatus = _GetModeAndStatus()

    If $serviceState = 1 Then
        ; The service is running
        _UpdateGUI($modeAndStatus, 1)
    ElseIf $serviceState = 0 Then
        ; The service has stopped
        _UpdateGUI($modeAndStatus, 0)
    Else
        ; Status retreival errored
        _UpdateGUI($modeAndStatus, -1)
    EndIf

    Sleep(60000) ; Wait one minute
WEnd

; This function updates the GUI, it takes the service starting mode and a status for the service, and a number for the state of the service (stopped / running / error )
Func _UpdateGUI($modeAndStatus, $serviceState)
    Local $statusText = ""

    Switch $serviceState
        Case -1 ; Error
            $statusText = "Error"
        Case 0 ; Stopped
            $statusText = "Stopped"
            GUICtrlSetData($hoststartstop_button, "Start")
        Case 1 ; Running
            $statusText = "Running"
            GUICtrlSetData($hoststartstop_button, "Stop")
    EndSwitch

    GUICtrlSetData($host_input, $statusText " / " & _GetMode($modeAndStatus))
    GUICtrlSetData($hostenabledisable_button, _GetStatus($modeAndStatus))
EndFunc   ;==>_UpdateGUI

Func _GetMode($ar)
    Return $ar[0]
EndFunc   ;==>_GetMode

Func _GetStatus($ar)
    Return $ar[1]
EndFunc   ;==>_GetStatus

Func _GetModeAndStatus()
    Local $startMode = ""
    Local $startStatus = ""

    $startMode = _GetHostServiceState()

    Switch $startMode
        Case 1
            $startMode = "System"
            $startStatus = "Disable"
        Case 2
            $startMode = "Automatic"
            $startStatus = "Disable"
        Case 3
            $startMode = "Manual"
            $startStatus = "Disable"
        Case 4
            $startMode = "Disabled"
            $startStatus = "Enable"
        Case Else
            $startMode = "Unknown"
            $startStatus = "Enable"

            _DisableGUI()
    EndSwitch

    Local $return[2] = [$startMode, $startStatus]

    Return $return
EndFunc   ;==>_GetModeAndStatus

Func _DisableGUI()
    GUICtrlSetState($hoststartstop_button, $GUI_DISABLE)
    GUICtrlSetState($hostenabledisable_button, $GUI_DISABLE)
    GUICtrlSetState($hostRestart_Button, $GUI_DISABLE)
EndFunc   ;==>_DisableGUI

Func _GetHostServiceState()
    Return RegRead("\\" & $serverName & "\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\alerter\", "Start")
EndFunc   ;==>_GetHostServiceState

Func _ServiceRunning($sServiceName)
    Local $arRet
    Local $hSC
    Local $hService
    Local $bRunning = 0

    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
            "str", "", _
            "str", "ServicesActive", _
            "long", $SC_MANAGER_CONNECT)
    If $arRet[0] <> 0 Then
        $hSC = $arRet[0]
        $arRet = DllCall("advapi32.dll", "long", "OpenService", _
                "long", $hSC, _
                "str", $sServiceName, _
                "long", $SERVICE_INTERROGATE)
        If $arRet[0] <> 0 Then
            $hService = $arRet[0]
            $arRet = DllCall("advapi32.dll", "int", "ControlService", _
                    "long", $hService, _
                    "long", $SERVICE_CONTROL_INTERROGATE, _
                    "str", "")
            $bRunning = $arRet[0]
            DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
        EndIf
        DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf

    Return $bRunning
EndFunc   ;==>_ServiceRunning

Thanks, I will check this out

I should have prefaced with the first 2 pieces of code were my code, and the last piece of code was someone elses function that I had found on the forums, sorry about that. I believe that's why the naming was so confusing, but I just cut and pasted their function and it worked, so I left it alone. I'll try yours now.

Link to comment
Share on other sites

I'm trying to figure out how to pass the variable through, to have the function interact with the proper button or inputbox associated variable name?

You'll probably have to update things like variable names, function names and there might be a few bugs. I couldn't test any of it so I just focussed on building a relatively neat structure to what you had, because that's what you asked for.

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