Jump to content
Valnurat

Return of error code to my exe file.

Recommended Posts

Valnurat

Is there any change that I can return a code for success or not success in my script?

I have a script that look in a SQL db and write to the registry.

I want to return some kind of a control, because I will call my script *.exe from VBScript.

_Main()

Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
EndFunc

 


Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites
benners

If you want to return a code from your AutoIt exe to the VB script you can set the exit value to what you want by using the exit function and setting the return code parameter

Exit (1)

 

Share this post


Link to post
Share on other sites
Valnurat

So I just do this:

_Main()

Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
    Exit(1)
EndFunc

 


Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites
water
1 hour ago, Valnurat said:

Is there any change that I can return a code for success or not success in my script?

To achieve this you would first need to decide if the function worked or didn't. Best way is to check every _ADO* function for an error code.
You now only check once (after _ADO_Connection_OpenConString). Do it after every call.

I would drop "Exit(1)" as this always ends your script. End the script in the main function.

_Main()
If @error Then Exit MsgBox(0, "Error", "Script ended with @error = " & @error)
; More code to execute
Exit

Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
EndFunc

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Valnurat

Ok, but how can I receive the status code if I call the exe file from VBScript?


Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites
water

Then you need Exit.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Valnurat

Like this:

_Main()
Exit(1)

Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
EndFunc

 

Edited by Valnurat

Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites
water

Would the calling VBS need to know if there was an error in your AutoIt script?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Valnurat

Yes, because I would like to create a txtfile with controlinfo if it fails.


Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites
water

Then you need to use something like this and return different values.
On success the return value is set to 0, else it holds the error code returned by any of the _ADO* functions.

_Main()
Exit(0)

Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then Exit(@error)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    If @error Then Exit(@error)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    If @error Then Exit(@error)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    If @error Then Exit(@error)
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
EndFunc

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Valnurat

It does return 0 as  you said, even thogh if $aRecordset_inner[0][0] is empty. :)

I would like to return a exitcode if $aRecordset_inner[0][0] is empty.

How can I do that?

EDIT: In my VBScript I check the registry and if it's empty I will run my exefile. Right now my exefile just return 0 no matter if $aRecordset_inner[0][0] is empty or not. I don't want to check the registry 2 times in my VBScript before creating a txtfile with controlinfo.

Edited by Valnurat

Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites
water
31 minutes ago, Valnurat said:

I would like to return a exitcode if $aRecordset_inner[0][0] is empty.

You already did half of the coding ;)

If $aRecordset_inner[0][0] = "" Then Exit(99) ; Or whatever return value you like

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Valnurat

Aha. Very simpel. :>

Thank you.


Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites
water

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Valnurat

Sorry, for returning to this ticket, but I don't understand why my VBScript is returning errrorcode 0, when I can see my AutoIT errorcode is 99.

; SetUP internal ADO.au3 UDF COMError Handler
_ADO_ComErrorHandler_UserFunction(_ADO_COMErrorHandler)

_Main()
Exit(0)


Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    if IsArray($aRecordset_inner) Then
        If $aRecordset_inner[0][0] = '' or $aRecordset_inner[0][0] = Null Then Exit(99)
        RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
    Else
        Exit(99)
    EndIf
EndFunc

Capture.JPG

My VBScript is this:

Sub RegComputerInfo
 	Const HKEY_CURRENT_USER = &H80000001
	Dim lstrKeyPath, lstrValueName, lstrValue, outFile, objFile, oExec
	lstrKeyPath = "SOFTWARE\ComputerInfo\"
	lstrValueName = "Shipdate"
	if ADHelper.IsCurrentUserMember("AllUsers") and (ADHelper.strComputerType <> "Server") then
		objReg.GetStringValue HKEY_CURRENT_USER,lstrKeyPath,lstrValueName,lstrValue
		if IsNull(lstrValue) then
			set oExec = WSHShell.exec(strScriptPath & "\Support\Script\ComInventory\GetShipdate.exe") ' Read a SQL DB
			msgbox "Test1 " & oExec.ExitCode		
			if oExec.ExitCode <> 0 then
				outFile = "\\servername\install\Log\ComputerInfo\" & WSHNetwork.ComputerName & ".txt"
				Set objFile = objFSO.CreateTextFile(outFile,True)
				objFile.Write strUsername & "|" & strModel & "|" & ServiceTag
				objFile.Close
			else
				msgbox "Test " & oExec.ExitCode		
			End if	
		End If	
	End if
End Sub 'FindShipDate

 


Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites
Valnurat

If I put this in my VBScript I do get my errorcode = 99

My VBScript is this:

Sub RegComputerInfo
 	Const HKEY_CURRENT_USER = &H80000001
	Dim lstrKeyPath, lstrValueName, lstrValue, outFile, objFile, oExec
	lstrKeyPath = "SOFTWARE\ComputerInfo\"
	lstrValueName = "Shipdate"
	if ADHelper.IsCurrentUserMember("AllUsers") and (ADHelper.strComputerType <> "Server") then
		objReg.GetStringValue HKEY_CURRENT_USER,lstrKeyPath,lstrValueName,lstrValue
		if IsNull(lstrValue) then
			set oExec = WSHShell.exec(strScriptPath & "\Support\Script\ComInventory\GetShipdate.exe") ' Read a SQL DB
			Do While oExec.Status = 0			<-----
     			WScript.Sleep 100				<-----
			Loop						<-----
			WScript.Echo oExec.Statusmsgbox "Test1 " & oExec.ExitCode		
			if oExec.ExitCode <> 0 then
				outFile = "\\servername\install\Log\ComputerInfo\" & WSHNetwork.ComputerName & ".txt"
				Set objFile = objFSO.CreateTextFile(outFile,True)
				objFile.Write strUsername & "|" & strModel & "|" & ServiceTag
				objFile.Close
			else
				msgbox "Test " & oExec.ExitCode		
			End if	
		End If	
	End if
End Sub 'FindShipDate

But should I really do that?


Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites
Valnurat

Sorry, if I step on some toes here, but is there someone who could help me?


Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites
Juvigy

Why do you need a VBS? Do everything with Autoit. It will be simpler and the problem with return codes will be gone!

Share this post


Link to post
Share on other sites
Valnurat

True, but as you can see in my VBS I create a txtfile on a server, if the exitcode is not 0.

In the future that server might not exists anymore and then I have to modify my autoit to the new server.

I would like to avoid to modify my autoit code.


Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites
water

Store the variable information (server address etc.) in an INI-file. So you can easily move to another server and only need to modify the config INI file.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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

  • Similar Content

    • JuanFelipe
      By JuanFelipe
      Cambiar a español Hello friends, I need help with a problem I have, I want to know if there is any way to update a GUI already running, that is I have a GUI of a certain size and with a button I would like to enlarge it:
      example: $ form1 = GUICreate ("", 200,100,0,0)   When you press the button, the gui increases its size, and actuates the new size.
    • BogdanNicolescu
      By BogdanNicolescu
      While 1 / / / A whole bunch of codes found in help and here: https://www.autoitscript.com/autoit3/docs/ / / / WEnd OR:

       
      HotKeySet("{ESC}", "Terminate") While 1 MouseClick("Right",674,422) MouseClick("Left",673,447) Sleep(2000) / / / A whole bunch of codes found in help and here: https://www.autoitscript.com/autoit3/docs/ / / / MouseClick("Left",675,339) SLeep(3000) WEnd Func Terminate()     Exit 0 EndFunc  
      Sorry if i should't let this here to be found by newbies like me -.-'
    • santoshM
      By santoshM
      Dear friends i am facing a problem , i have created a window in autoit some time it is moving with the mouse curser even if iam not clicking the window 
    • sumanth1980
      By sumanth1980
      Here i am wrote a simple lines to install basic applications but when i clicked on one application/program to install it was working fine but when i cancel the program in the middle to install another program it was not working. I am not an expert in this but i want a solution for this ... Can any one help me on this....
      ORG-1.au3
    • PleaseHelpMEIWillLoveyou
      By PleaseHelpMEIWillLoveyou
      Is there a way to use a custom terminal or something from my pc and run a script on another pc.
      please let me know if its possible
      (not for anything bad just for a project im working on)
×