Question

I would like to customize the exit code of my elevated trust, Out of Browser (OOB) Silverlight 4 application. I'm currently attempting to use the System.Environment.ExitCode property to customize the exit value of my SL4 app, however, it appears that it is being overriden by sllauncher.exe and always returns 0.

Here are the only changes made to a default SilverlightApplication generated by Visual Studio, outside of adjustments to the project properties to enable OOB Elevated Trust:

At MainPage.xaml.cs:

public MainPage()
{
    InitializeComponent();
    Environment.ExitCode = 42;
}

Also updated App.xaml.cs as a precaution:

private void Application_Exit(object sender, EventArgs e)
{
    Environment.ExitCode = 42;
}

Once the XAP is installed to the local system, I'm using the "start /wait" syntax via command prompt to launch the OOB app and ensure that the exit code of the Windowed application is set, ie:

start /wait sllauncher.exe 1899735003.localhost

After closing the app, thereby returning focus to the command prompt, and running:

echo Exit Code is %errorlevel%

The Exit Code is always set to 0.

Is there a way to have sllauncher set an exit code provided by the OOB app?

Was it helpful?

Solution

I managed to do it by calling the TerminateProcess Windows API call: (don't do this!)

    [DllImport("kernel32.dll")]
    static extern int TerminateProcess(IntPtr processIdOrHandle, uint exitCode);

    [DllImport("kernel32.dll")]
    static extern IntPtr GetCurrentProcess();

    public static void Exit(uint code) {
         TerminateProcess(GetCurrentProcess(), code);
    }

The code makes me very nervous, however: TerminateProcess is a rather nasty "immediate kill switch"-kind of call that will immediately kill sllauncher.exe. If sllauncher.exe usually does any kind of cleanup after a Silverlight application is closed, which I assume it might, that cleanup is now omitted. I'm very wary of taking this approach.

I figured that another way to "communicate" with the parent process is by writing something of an exit code to a temporary file, however Silverlight's Environment.GetSpecialFolder call does not give me access to anything like a temp directory.

I'm probably going to have to P/Invoke to the Windows API to find the temporary directory, and then write a file to it, and read that out from the parent process.

(note: this was Silverlight 5. I'm not sure to what extent you can P/Invoke at all on SL4)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top