gte Posted November 13, 2009 Share Posted November 13, 2009 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. expandcollapse popupif _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 HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script Link to comment Share on other sites More sharing options...
Bert Posted November 13, 2009 Share Posted November 13, 2009 Look at Select/Case/EndSelect The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
jvanegmond Posted November 13, 2009 Share Posted November 13, 2009 (edited) 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 !!expandcollapse popupWhile 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 November 13, 2009 by Manadar github.com/jvanegmond Link to comment Share on other sites More sharing options...
gte Posted November 13, 2009 Author Share Posted November 13, 2009 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 !! expandcollapse popupWhile 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. HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script Link to comment Share on other sites More sharing options...
jvanegmond Posted November 13, 2009 Share Posted November 13, 2009 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. github.com/jvanegmond Link to comment Share on other sites More sharing options...
gte Posted November 13, 2009 Author Share Posted November 13, 2009 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. HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script Link to comment Share on other sites More sharing options...
jvanegmond Posted November 13, 2009 Share Posted November 13, 2009 I have no idea what you mean. Wasn't my rewrite inspirational enough? github.com/jvanegmond Link to comment Share on other sites More sharing options...
gte Posted November 16, 2009 Author Share Posted November 16, 2009 Your rewrite was great, but I'm not sure how to call the same function, yet update different guictrls with it?I have no idea what you mean. Wasn't my rewrite inspirational enough? HP OpenView ServiceCenter keep alive scriptRemote Desktop Login Script Link to comment Share on other sites More sharing options...
jvanegmond Posted November 16, 2009 Share Posted November 16, 2009 Pass a reference to a button in the method. Func _GUIUpdateButton($hButton, $text) GUICtrlSetData($hButton, $text) EndFunc github.com/jvanegmond Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now