How to return a custom code/value from compiled EXE

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
TXTechie
Posts: 13
Last visit: Sun Oct 04, 2020 7:40 pm

How to return a custom code/value from compiled EXE

Post by TXTechie »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: 5.4.144
32 or 64 bit version of product: 32
Operating system:
32 or 64 bit OS: 32 & 64

*** Please add details and screenshots as needed below. ***


I searched through the PowerShell Studio and GUI forums and couldn't find an answer to my question. I'm still learning PowerShell and this is the first PowerShell Studio GUI and executable that I've attempted to create.

I'm creating a very simple GUI which requires the user to choose either 'Yes' or 'No' buttons. I've already got that working, my question is:
  • How can I pass a custom return value or code (for example: 1 for 'Yes' and 2 for 'No') from the PowerShell executable back to the calling application; which in this case is a batch file - so that I can do something in the batch file based on the response of the user?
I appreciate any and all assistance I can get.

Regards,

TX Techie

cody m

Re: How to return a custom code/value from compiled EXE

Post by cody m »

Hello,
Your best bet would be to write to a file or a registry.

cody m

Re: How to return a custom code/value from compiled EXE

Post by cody m »

Quick Correction
There is another way to return a value from your EXE, you can use the $script:ExitCode variable, and that gets stored in %errorlevel%.

User avatar
mxtrinidad
Posts: 399
Last visit: Tue Jan 19, 2021 10:16 am

Re: How to return a custom code/value from compiled EXE

Post by mxtrinidad »

Check one of my old blog post might add some light: http://www.maxtblog.com/2011/09/creatin ... s-package/

:)

User avatar
TXTechie
Posts: 13
Last visit: Sun Oct 04, 2020 7:40 pm

Re: How to return a custom code/value from compiled EXE

Post by TXTechie »

cody m wrote:
Fri Sep 29, 2017 4:14 pm
Quick Correction
There is another way to return a value from your EXE, you can use the $script:ExitCode variable, and that gets stored in %errorlevel%.
Hi Cody,

I appreciate your assistance. It looks like one additional change needs to be made in order for your suggestion to work properly. The following line of code needs to be at least commented out near the end of the Startup.pss file, otherwise the return code from the executable is always 0 (zero), no matter what I set $script:ExitCode to in the script code of the executable:

Code: Select all

$script:ExitCode = 0 #Set the exit code for the Packager
Again, thank you for the assistance, Cody!

Regards,

TXTechie

User avatar
TXTechie
Posts: 13
Last visit: Sun Oct 04, 2020 7:40 pm

Re: How to return a custom code/value from compiled EXE

Post by TXTechie »

mxtrinidad wrote:
Sun Oct 01, 2017 11:56 am
Check one of my old blog post might add some light: http://www.maxtblog.com/2011/09/creatin ... s-package/

:)
Hi Max,

I really appreciate your assistance. Since it does not require me to make any additional changes to the default PowerShell Project files (such as to the Startup.pss file, from Cody's suggestion), I will use the 3rd suggestion from the blog post that you shared:

Code: Select all

[Environment]::Exit("N")
Where "N" is the exit code number (integer) that you want to send to the calling routine to your PowerShell Executable.

Thank you, very much, Max!

Regards,

TX Techie

User avatar
Alexander Riedel
Posts: 7665
Last visit: Wed Apr 21, 2021 12:28 am
Answers: 3
Been upvoted: 8 times

Re: How to return a custom code/value from compiled EXE

Post by Alexander Riedel »

The line in startup.pss is there to be modified by you to return any code you like, based on the needs of your script or application.
Using $ExitCode is the prescribed way of returning a value to a parent process.
The "[Environment]::Exit("N")" call terminates the process at that exit location. No further clean up and termination routines (or any other code elements) are executed. It is so to speak an ejection seat :D and should only be used in grave error conditions. Best practices suggest that the exit of an application should always happen controlled at the same location.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
TXTechie
Posts: 13
Last visit: Sun Oct 04, 2020 7:40 pm

Re: How to return a custom code/value from compiled EXE

Post by TXTechie »

Thank you for the correction, Alexander. I also see that it's slower than using $ExitCode.

I'm trying to use this in a way that meets best practices and requires the least amount of changing the defaults.

Is there a way I can simply edit the line in the Startup.pss file to something like this $script:ExitCode = $returnValue, and then having my 'Yes' and 'No' buttons set the $returnValue to my custom exit codes? When I do this it still defaults to 0 (zero) no matter what I do.

How would you recommend being the best way for me to do this?

User avatar
Alexander Riedel
Posts: 7665
Last visit: Wed Apr 21, 2021 12:28 am
Answers: 3
Been upvoted: 8 times

Re: How to return a custom code/value from compiled EXE

Post by Alexander Riedel »

Yes, that would be a good way. You probably missing the scope. Your $returnValue is likely two variables of the same name in your script at different scopes. PowerShell's default scope is a little hard to understand sometimes. Use $script:returnValue in both places, that should do it.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
TXTechie
Posts: 13
Last visit: Sun Oct 04, 2020 7:40 pm

Re: How to return a custom code/value from compiled EXE

Post by TXTechie »

That did it!

Thank you, very much, Alexander!

Regards,

TX Techie