janisito
Posts: 78
Joined: Thu Apr 09, 2015 6:28 am

Catch installer error code

Hi,

In my installer I'm using the custom action LaunchFileFromProperty to install SQL Server Express 2017 with command line parameters in quiet mode. This happens when the user presses the install button in my last installer dialog. Most of the times it works fine but occasionally the SQL Server Express 2017 installation package returns an error code (3010) that is needs a reboot (there have been other error codes as well, can't remember which though) and my installation stops. I know I can uncheck the Fail installation if custom actions returns an error but I rather have my installer check the error code and continue based on what error code it is. Is this at all possible? How would I go about to catch that error code?

Regards
Jan
janisito
Posts: 78
Joined: Thu Apr 09, 2015 6:28 am

Re: Catch installer error code

Here is my solution to get the exit code. Implemented a custom action that runs this code:

ProcessStartInfo processStartInfo = new ProcessStartInfo();

processStartInfo.CreateNoWindow = false;
processStartInfo.UseShellExecute = false;
processStartInfo.FileName = filename;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.Arguments = commandLine;

using (Process process = Process.Start(processStartInfo))
{
process.WaitForExit();
exitCode = process.ExitCode;
}
Catalin
Posts: 6586
Joined: Wed Jun 13, 2018 7:49 am

Re: Catch installer error code

Hello Jan,

Well, this is how I think you can achieve what you want. First of all, we will need a wrapper for your EXE (e.g. another EXE, DLL, script file) which will launch your EXE, take its error code and, based on that, returns a specific value and after all that, depending on the return code of the script (which depends on the return code of your EXE), we will fail the installation of the main package or not.

Here is a small PowerShell script example which I hope will makes things more clear:

Code: Select all

$a = Start-Process -FilePath "PathToYourExe" -PassThru -Wait


if ($a.ExitCode -eq 1)
   {return 1}

if ($a.ExitCode -eq 0)
   {return 0}
As you can notice above, we launch the process of your EXE, wait for it to finish, take its exit code and, based on that, we control the return code of our script. You can easily test the script above using the "notepad.exe" process. Simply replace "PathToYourExe" with "notepad.exe" and test the following cases:

1- close the notepad by pressing the "x" button
2- open Task Manager --> "Details" tab --> select "notepad.exe" --> right click + "End process tree"

In the second case, the script will return 1 (because the notepad.exe was stopped unexpectedly and forcefully --> returning 1 and in the first case, it will return 0 (because the notepad.exe was normally stopped).

After doing all this, you can save the script with the following format (script.ps1) and then use a "LaunchFile" custom action. Please be aware that the script must return 0 for success or any other value for failure, so please be aware of the if statement you are using.

Hope this helps.

All the best,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”