Jump to content
Sign in to follow this  

Return Values

Recommended Posts

I am trying to learn how to take advantage of the return values in functions but I don't understand how they work or how I could use them for say error catching in a script. Below is a function that was made by engine and I want to learn how to use the return values to see if the operation was a success or not.



; Name...........: _Service_Start
; Description ...: Starts a service.
; Syntax.........: _Service_Start($sServiceName [, $sComputerName])
; Parameters ....: $sServiceName - Name of the service.
;                  $sComputerName - [Optional] The name of the target computer. The local computer is default.
; Requirement(s).: Administrative rights on the target computer.
; Return values .: Success - 1
;                  Failure - 0
;                            Sets @error

; Author ........: engine
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......:


So in my script I test to see if a service is running or not and if it is not then I use the service start to get it to run.


Local $sServiceName = "Audiosrv"

Func ServiceStatus()
MsgBox(0, "Checking", "Checking the status of the service.")

Case $aiStatus[1] = $SERVICE_STOPPED
Case $aiStatus[1] = $SERVICE_RUNNING

The function says it returns a value of 1 for Success and 0 for Failure so I am wondering how or if I could use that to make sure that whatever action I performed on the service actually worked and if not maybe display a msgbox that states that.


Thank you in advance for your time and patience.

Edited by Nunos
Highlighted the part I am trying to learn to use.

Share this post

Link to post
Share on other sites

You have to assign the return value to a variable. Then you can use the variable for whatever you want.


$iReturn = _Service_Start($sServiceName)
If $iReturn = 1 Then
    ; Success, do something now
ElseIf $iReturn = 0 Then
    ; Failure, do something else

In this case the function passes its return value to the variable $iReturn. Since 1 means success and 0 failure you can easily check if the function failed or not by checking the variable. Hope this helps!

Edited by Floops

Share this post

Link to post
Share on other sites

Just to clarify, you do not have to assign the return to a variable, you can work with it directly:

MsgBox(0, "Result", "Function result was " & ((_Service_Start("AxInstSV") = 0) ? "failure" : "successful"))


Share this post

Link to post
Share on other sites

Thank you for the clarification. 

I will test tonight after work.

One question what is AxInstSV ? I have never seen that before. Would I need to change that to the name of the service I am testing it on or perhaps the variable I stored the service name in?



Edited by Nunos

Share this post

Link to post
Share on other sites

MsgBox(0, "Result", "Function result was " & ((_Service_Start($sServiceName) = 0) ? "failure" : "successful"))

Does that look correct?


Share this post

Link to post
Share on other sites

Sorry you guys are going faster than me. I tested and it seems to work. I will test at home tonight after work on the computer with the actual service I am trying to check it is an sql instance on a server that after reboots doesn't start fast enough. 


Thank you all for your help. :D

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
Sign in to follow this  

  • Create New...