Jump to content

last return value of a function(not return code)


Recommended Posts

Hello ! I have an issue about capturing the last returned value from a function into another script (not the return code ( 0 for success or higher than 0 if failure)).

I looked up about any way to solve this and didn't find on this forum or other. I have 2 scripts - Script 1 and Script 2. Script 1 is a function that return a variable. I put a basic example (which contains a value - that value I want to catch ). I compile the script and this way an .exe is generated. In the second script I do something like : x= Run ("full path of exescript1.exe) and I display x. Apparently x has the value of an id which I don't understand where it comes from. It is the PID but I don't need this. With RunWait I capture the return code.

Is any way I could do this (in my case the value is 15)? I figured I can take RUN or RUNWAIT's UDF and get the code from here,copy it in my own place, modify in my own function RUN1 for example and modify it to capture the last return value, but I don't find the UDF for RUN or RUNWAIT. Any thoughts?

I attached an example of what I want to do. ...I want $x from Script 2 to be equal with 15.

Script1.au3

Script2.au3

Edited by Melba23
Fixed formatting
Link to comment
Share on other sites

  • Moderators

AndrewClim,

Please pay attention to where you post - the "Dev Chat" section where you started this thread is not for general support questions. I have moved it for you (and fixed the formatting) but would ask you to be more careful in future. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Theres a few ways to do this but the best is something like:

Run("cmd.exe /c script2.exe " & Returnval())

Func returnval()

;some code

Local $val = 1

Return $val

Endfunc

In script2.exe you say:

$CmdLine[0]

I'm typing from my phone so there's prob some mistakes. $CmdLine variable is reserved in auto it for this kind of stuff.

Link to comment
Share on other sites

  • Moderators

AndrewClim,

The easiest way to do this would be to use a temporary ini file to store the return value - but you must use RunWait on the compiled file to give it a chance to write the file before the second script tries to read it. Otherwise you are into "inter process communication" - there are lots of ways to do this if you search, but they are all considerably more trouble to code than a simple ini file. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

 I did this : 

Func _Main()
 
local $a=45
if $cmdline[0] >0 then
   if $cmdline[1] <>@ScriptName Then
$a= $cmdline[1]
   EndIf
EndIf
MsgBox(0,"" ,$a)
return $a
 
EndFunc
_Main()
 
 
I compiled this .Now I have an .exe that I run from CMD .What should I do in the other script where I have to capture that $a value ? I can't make the connection.
 
Melba23 ,I am not allowed from specifications to make a ini file,text file ,or other file to store that returned value that's why I searched for any other way around it . Thank you anyway for suggestion and sorry for posting in other section in the first place . 
Edited by AndrewClim
Link to comment
Share on other sites

  • Moderators

AndrewClim,

 

,I am not allowed from specifications to make a ini file,text file ,or other file to store that returned value

Then IPC is the way to go - my personal favourite is trancexx's MailSlot. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

apparently, Windows does keep the exit code of a process in memory after the process terminated - although i can't tell for how long. you can query the exit code of a terminated process if you have it's PID:

;===============================================================================
;
; Function Name:    _ProcessExitCode()
; Description:      Returns a handle/exitcode from use of Run().
; Parameter(s):     $i_Pid        - ProcessID returned from a Run() execution
;                   $h_Process    - Process handle
; Requirement(s):   None
; Return Value(s):  On Success - Returns Process handle while Run() is executing
;                                (use above directly after Run() line with only PID parameter)
;                              - Returns Process Exitcode when Process does not exist
;                                (use above with PID and Process Handle parameter returned from first UDF call)
;                   On Failure - 0
; Author(s):        MHz (Thanks to DaveF for posting these DllCalls in Support Forum)
;
;===============================================================================
Func _ProcessExitCode($i_Pid, $h_Process_input = 0)
    ; 0 = Return Process Handle of PID else use Handle to Return Exitcode of a PID
    Local $v_Placeholder
    If $h_Process_input=0 Then
        ; Return the process handle of a PID - this is for the initial check
        $h_Process = DllCall('kernel32.dll', 'ptr', 'OpenProcess', 'int', 0x400, 'int', 0, 'int', $i_Pid)
        If Not @error Then Return $h_Process[0]
    Else
        ; Return Process Exitcode of PID - this is for ending check
        $h_Process = DllCall('kernel32.dll', 'ptr', 'GetExitCodeProcess', 'ptr', $h_Process_input, 'int*', $v_Placeholder)
        If Not @error Then Return $h_Process[2]
    EndIf
    Return 0
EndFunc   ;==>_ProcessExitCode

credits go to MHz here, although i can't seem to locate the original post (this code is a bugfix of mine over his function).

how to use:

1) call Run and save the PID

2) immediately call _ProcessExitCode using the PID to retrieve a process handle

3) whenever you wish, use ProcessExists() to check... well... if your process exists. if it does not exist, call _ProcessExitCode again with the PID and the handle to get the exit code.

note: if you get exit code = 1 then it means either that the process returned 1, or it was terminated by external means (e.g. killed via Task Manager). so, make sure your process does not ever return 1 as a valid exit code, and you're good to go.

EDIT: you'll need to close the handle when you're done:

DllCall('kernel32.dll', 'ptr', 'CloseHandle', 'ptr', $h_Process)

$h_Process is the handle you got in step 2

Edited by orbs

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Melba23 ,I am not allowed from specifications to make a ini file,text file ,or other file to store that returned value that's why I searched for any other way around it . Thank you anyway for suggestion and sorry for posting in other section in the first place . 

I'm guessing read and write to the Registry is also a no no.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

  • Moderators

AndrewClim,

This works fine for me. :)

Script 1:

#include "MailSlot.au3"

; Create mailslot name
$sMailSlotname = "mymailslot"
$sMailSlotName_Receive = "\\.\mailslot\" & $sMailSlotname

; Cretae MailSlot
$hMailSlot = _MailSlotCreate($sMailSlotName_Receive)
If @error Then
    MsgBox(48, "MailSlot Script 1", "Failed to create MailSlot account")
    Exit
EndIf

; Run second script - passing MailSlot to use
RunWait("Script2.exe " & $sMailSlotName_Receive)

; Read message passed
$iSize = _MailSlotCheckForNextMessage($hMailSlot)
If $iSize Then
    $sData = _MailSlotRead($hMailSlot, $iSize, 1)
    If $sData Then
        MsgBox(48, "MailSlot Script 1", "MOMENT OF TRUTH" & @CRLF & $sData)
    Else
        MsgBox(48, "MailSlot Script 1", "No data returned")
    EndIf
Else
    MsgBox(48, "MailSlot Script 1", "MailSlot is empty")
EndIf

; Close MailSlot
_MailSlotClose($hMailSlot)
Script 2:

#include "MailSlot.au3"

$a = 45

; Get mailslot address for calling script
$sMailSlotSend = $CmdLine[1]

; If address passed - send mail
If $sMailSlotSend = "" Then
    MsgBox(48, "MailSlot Script 2", "No MailSlot passed")
Else
    _MailSlotWrite($sMailSlotSend, String($a)) ; Send as string <<<<<<<<<
EndIf
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

It worked .Apparently I had a different older version of Autoit because when I tried a method with cmdline ,it gave me an error,always the same:    "Array variable has incorrect number of subscripts or subscript dimension range exceeded.($sMailSlotSend = $CmdLine[1]

$sMailSlotSend = ^ ERROR)" ,also when I run your script Melba23 it gives me the same error   So that part was missing from my MailSlot script because of this error so it was normal not work . But now it does ,thank you very much all of you ,especially Melba23 ! Topic can now be closed :) 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...