Get exit code from Winforms packaged EXE

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
hopkinsnd
Posts: 9
Last visit: Thu Apr 07, 2022 2:41 pm

Get exit code from Winforms packaged EXE

Post by hopkinsnd »

I have a WinForms project - let's call it Child.exe. Child.exe is called from another compiled powershell script - let's call it Parent.exe. I am trying to retrieve the exist code from Child.exe and do something with that in Parent.exe.

In Child.exe I have tried:
$global:exitCode = 0
$form.Close()

From Parent.exe $LASTEXITCODE is blank.

In Parent.exe I have tried:
$var = & Child.exe
wait-process -Name Child
$var is empty after Child.exe closes.

Product: PowerShell Studio 2021 (64 Bit)
Build: v5.8.190
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.19042.0
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get exit code from Winforms packaged EXE

Post by jvierra »

Only the "exit" command can set the exit code.

The following discussion should give you a handle on how this works.

https://stackoverflow.com/questions/502 ... ell-script
User avatar
Alexander Riedel
Posts: 8473
Last visit: Tue Mar 19, 2024 1:15 am
Answers: 19
Been upvoted: 37 times

Re: Get exit code from Winforms packaged EXE

Post by Alexander Riedel »

$ExitCode is the correct variable to set in your child process.

It seems there is a bug in PowerShell that prevents you from getting the exit code of a child process
with separate wait.
It is illustrated here:
https://stackoverflow.com/questions/102 ... ad-of-wait

Depending on your needs, the following post may have some solutions:
https://stackoverflow.com/questions/102 ... ad-of-wait
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 2 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked