Sign in to follow this  
Followers 0
Nunos

Return Values

9 posts in this topic

#1 ·  Posted (edited)

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

Select
Case $aiStatus[1] = $SERVICE_STOPPED
    _Service_Start($sServiceName)
    
Case $aiStatus[1] = $SERVICE_RUNNING
     _Service_Resume($sServiceName)
     
EndSelect
EndFunc

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



#2 ·  Posted (edited)

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
EndIf

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

#3 ·  Posted

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"))

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

#5 ·  Posted

2 minutes ago, Nunos said:

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?

Yes that's what you need to do.


UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Share this post


Link to post
Share on other sites

#6 ·  Posted

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

#7 ·  Posted

AxnstSV was just a service I grabbed as an example, it is the ActiveX installer service. And yes, you would simply change the service name to the one you want to start/stop.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

@Nunos Try it :) Does it work as you expect?

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#9 ·  Posted

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  
Followers 0