繁体   English   中英

从托管代码启动 Windows 沙箱

[英]Starting the Windows Sandbox from managed code

我正在尝试以编程方式初始化Windows 沙箱 我的目标是生成一个.wsb配置文件,然后启动它。 目前我正在使用Process.Start()方法,但我不断收到Start()方法的错误。

这是代码:

var sbProc = new Process();
sbProc.StartInfo.FileName = configPath;
sbProc.Start();
sbProc.WaitForExit();

抛出: System.ComponentModel.Win32Exception: 'Application not found'

我确定该文件存在,因为我尝试通过双击并通过命令行打开它。 两者都按预期工作,所以我假设它指的是相关的应用程序(在本例中为 Windows 沙盒)。

我尝试添加:

sbProc.StartInfo.UseShellExecute = false;

抛出: System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.' .

这是相同的异常,但消息不同,这实际上非常令人困惑。 如上所述,我 100% 确定我的操作系统支持此功能; 每个要求都得到支持和启用。 Process.Start()是否有可能无法处理.wsb文件,如果是这样,我该如何实现我想要的

我愿意接受任何建议,并在此先感谢!

更新:

我尝试将动词更改为Invoke并使用以下代码Start

sbProc.StartInfo.Verb = "Start";

sbProc.StartInfo.Verb = "Invoke";

抛出: System.ComponentModel.Win32Exception: 'No application is associated with the specified file for this operation'

如何将文件关联和应用?

Windows 沙盒通过在资源管理器中双击并通过cmd.exe /c start ,但无法在 C# 应用程序中以编程方式启动。 原因是 C# 应用程序是 32 位的,而操作系统是 64 位的。

当 32 位应用程序尝试调用沙盒配置时,它(间接)尝试调用%windir%\System32\WindowsSandbox.exe可执行文件。 但! 当 32 位应用程序尝试访问 64 位 Windows 上的%windir%\System32时, 它会被重定向到包含 32 位版本的系统应用程序和 DLL 的%windir%\SysWOW64 由于没有 32 位WindowsSandbox.exe ,因此 .NET 框架抛出异常System.ComponentModel.Win32Exception: 'Application not found'

从 32 位用户应用程序启动 64 位系统应用程序有两种方法。

A计划

使用%windir%\Sysnative路径。 它仅适用于 32 位应用程序,并解析为真正的 64 位System32系统文件夹。

var sbProc = new Process();
sbProc.StartInfo.FileName = "%SystemRoot%\Sysnative\WindowsSandbox.exe";
sbProc.StartInfo.Arguments = "\"" +configPath + "\"";
sbProc.StartInfo.UseShellExecute = true;
sbProc.Start();
sbProc.WaitForExit();

我想,在这种情况下, WaitForExit将等到沙箱完成。 我不是 100% 确定,Windows 中可能会发生一些事情,但请期待。

B计划

计划 A 有以下风险:如果将来 MS 人为沙箱重命名可执行文件或添加额外的 CLI 开关,直接调用可能会失败。 您将不得不修改您的代码。

为避免这种情况,您可以使用cmd.exe通过start命令启动沙箱。 cmd.exe可以在幕后调用 64 位系统应用程序。

var sbProc = new Process();
sbProc.StartInfo.FileName = "cmd";
sbProc.StartInfo.Arguments = "/c start \"\" \"" +configPath + "\"";
sbProc.StartInfo.Start();
sbProc.WaitForExit();

唯一的问题是WaitForExit将在start完成后立即返回,而不是沙箱。 start不等待启动的进程。

暂无
暂无

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

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