简体   繁体   English

返回自定义Silverlight OOB应用程序ExitCode

[英]Return Custom Silverlight OOB Application ExitCode

I would like to customize the exit code of my elevated trust, Out of Browser (OOB) Silverlight 4 application. 我想自定义增强信任的退出代码(Out of Browser(OOB)Silverlight 4应用程序)。 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. 我当前正在尝试使用System.Environment.ExitCode属性来自定义我的SL4应用程序的退出值,但是,它似乎被sllauncher.exe覆盖,并且始终返回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: 这是对Visual Studio生成的默认SilverlightApplication所做的唯一更改,但对项目属性进行了调整以启用OOB提升信任:

At MainPage.xaml.cs: 在MainPage.xaml.cs中:

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

Also updated App.xaml.cs as a precaution: 还更新了App.xaml.cs作为预防措施:

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: XAP安装到本地系统后,我将通过命令提示符使用“ start / wait”语法来启动OOB应用程序,并确保已设置Windowed应用程序的退出代码,即:

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. 退出代码始终设置为0。

Is there a way to have sllauncher set an exit code provided by the OOB app? 有没有办法让Sllauncher设置OOB应用提供的退出代码?

I managed to do it by calling the TerminateProcess Windows API call: ( don't do this! ) 我设法通过调用TerminateProcess Windows API调用来做到这一点:( 不要这样做!

    [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 . 但是,代码使我非常紧张: TerminateProcess是一种相当讨厌的“立即TerminateProcess开关”类调用,它将立即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. 如果sllauncher.exe通常在关闭Silverlight应用程序后执行任何类型的清理(我认为可能sllauncher.exe ,则现在将省略该清理。 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. 我认为与父进程“通信”的另一种方法是将退出代码写到临时文件中,但是Silverlight的Environment.GetSpecialFolder调用不能使我访问临时目录。

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. 我可能需要P / Invoke到Windows API,以找到临时目录,然后向其中写入文件,并从父进程中读出该文件。

(note: this was Silverlight 5. I'm not sure to what extent you can P/Invoke at all on SL4) (注意:这是Silverlight5。我不确定您可以在多大程度上在SL4上P /调用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM