Valnurat

Return of error code to my exe file.

23 posts in this topic

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



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

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
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - 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

#5 ·  Posted

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

#6 ·  Posted

Then you need Exit.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - 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

#7 ·  Posted (edited)

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

#8 ·  Posted

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - 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

#9 ·  Posted

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

#10 ·  Posted

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - 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

#11 ·  Posted (edited)

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

#12 ·  Posted

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - 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

#13 ·  Posted

Aha. Very simpel. :>

Thank you.


Yours sincerely

Kenneth.

Share this post


Link to post
Share on other sites

#14 ·  Posted

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - 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

#15 ·  Posted

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

#16 ·  Posted

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

#17 ·  Posted

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

#18 ·  Posted

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

#19 ·  Posted

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

#20 ·  Posted

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - 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

    • Miliardsto
      By Miliardsto
      Hello I got these tabs
       
      Global $TabMain = GUICtrlCreateTabItem("Main") Global $Action = GUICtrlCreateTabItem("Actions") Global $TabThings = GUICtrlCreateTabItem("Things") As we know to change tab we have to click with mouse on tab name like here it looks like below.
      Is there possibility to change tab without click on the tab but change by click on button for example?

    • sree161
      By sree161
      Hi,
      i have a code which generates two numeric outputs.i have to save them in excel.
      Is there any way to export them to specific cells in excel???
    • TotalAuto
      By TotalAuto
      Howdy All
      I've gone around and around in circles wasting hours trying to fix this problem to no avail so I thought I would try posting for help given there are some real smart people in this forum.
      Background
      I have 5 separate .au3 scripts that all do different basic tasks. For example:
      1.au3 = opens Chrome browser to obtain information from a specific website then closes
      2.au3 = opens an Excel workbook to run a macro then saves and closes
      3.au3 = Open two seperate workbooks and transfers data from one to the other then saves and closes
      4.au3 = .......etc
      5.au3 = .......etc
      These 5 separate scripts, doing completely different tasks, need to each run once per day at 5 different designated times within a day and then close. For example:
      1.au3 = 10am
      2.au3 = 12pm
      3.au3 = 2pm
      4.au3 = 4pm
      5.au3 = 5pm
      Problem
      I can not get these different scripts to reliably run at their designated times. When I manually run these scripts individually by using 'Go F5' they work perfectly. If I convert these scripts to an .exe and manually run each one they work about 98% of time.
      How can I schedule these different .au3 files or associated .exe files to reliably run each day at different times?
      Attempts To Fix The Issue
      1. I have tried converting each .au3 script into their own .exe file and use Window's Task Scheduler to run these .exe files at their designated time but the success rate  of running each .exe in full, to completion, without error is very low at about 30%. Things that commonly go wrong include a browser not opening but movement still continues on the desk top. Other times things just do not happen at all.
      2. I have Windows 7 64 bit and have tried creating and running both (x86) and (x64) .exe files but again reliability problems exist;
      3. I have also tried moving each .exe file to the My Documents folder as I read someone that keeping your files on your desktop ties your files to individual Windows user profiles which can cause AutoIt operating errors. Not sure if this is correct but thought I would try it anyway. It did not fix my issue.
      4. I installed a program called AlwaysUp which supposedly always reliably runs in the background and will reliably execute your AutoIt script or .exe files. Still have significant reliability issues.
      Therefore, can someone please provide advice on how I can get my 5 different .au3 files to run at 5 designated times through the day, every day, without fail? Overtime I will be creating more and more different .au3 files to run certain tasks throughout the day and do not want to create one large AutoIt file that continually runs and attempts to do all the different tasks itself.
      Any help is very appreciated.
      Thanks
      Dan
       
       
    • hugomito
      By hugomito
      Hi all,
      I built an AutoIt application that interacts with several other applications/software. Now, it's turn to interfact with Visual Studio Team Services (VSTS).
      Does anyone has a blog, demo, script to share?
      Thanks!
    • Viki
      By Viki
      This is my first time here so please dont bombard me that what a silly question I am asking!!
      I have 500 rows (A1:A500) in a spreadsheet and I just want to copy one by one row and then paste into another application and then press enter, loop should repeat this until finishes all 500 rows.
      I have looked at clipget(), clip(put() but dont know how to select next row in next turn. I also looked at Array to store but again no luck. Can some guide me please..