簡體   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