如何从WSL(Ubuntu)Bash运行Windows可执行文件 [英] How can I run a Windows executable from WSL (Ubuntu) Bash
问题描述
随着2016年夏季的 Windows 10周年更新,可以在新的
Along with Windows 10 Anniversary update for summer 2016, came the possibility to run ubuntu binaries inside the new Windows Subsystem for Linux (WSL), a "lightweight" virtualized subsystem.
不幸的是,启动 C:\ Windows \ System32 \ bash.exe
,另一个 bash
ELF二进制文件在WSL中启动了一个进程,您无法从该进程中逃脱!您只能启动其他ELF二进制文件.
Unfortunately, launching C:\Windows\System32\bash.exe
, another bash
ELF binary starts a process inside the WSL, from where you cannot escape! You may launch only other ELF binaries.
那么如何从Windows Bash执行 *.exe
文件?[1]
So how can I execute *.exe
files from Windows Bash?[1]
[1]在 Microsoft的官方" GH支持存储库中也提出了问题.
推荐答案
本机解决方案
Windows 10 Insider预览更新(14951)基于几乎被遗忘的 binfmt_msc 用于启动二进制文件的Linux工具. binfmt_misc 的注册命令是这样的(其中/init
是win可执行文件的临时 binfmt_misc 解释器"):
Native solution
The official solution provided with Windows 10 Insider Preview Update (14951) is based on the almost forgotten binfmt_msc Linux facility for launching binaries. The registration command for the binfmt_misc would be like this (where /init
is the provisional binfmt_misc "interpreter" for the win-executables):
sudo echo ":WSLInterop:M::MZ::/init:" > /proc/sys/fs/binfmt_misc/register
然后将像常规程序一样启动win-executable:
And then win-executable would be launched like regular programs:
$ export PATH=$PATH:/mnt/c/Windows/System32
$ notepad.exe
$ ipconfig.exe | grep IPv4 | cut -d: -f2
$ ls -la | findstr.exe foo.txt
$ cmd.exe /c dir
并非所有win可执行文件都必须驻留在Windows( DrvFs )文件系统中-而不是Linux的文件系统( VolFs )-才能继承正确的 Windows 工作目录.
Not that any win-executable must reside in the windows (DrvFs) file-system - not on the Linux's file-system (VolFs) - in order to inherit a proper Windows working-directory.
cbwin 替代
直到获得最新版本,项目 cbwin 提供了一种解决方法,方法是在其中安装3个新的linux命令WSL:
The cbwin alternative
Untill you get the latest build, project cbwin offers a workaround, by installing 3 new linux commands inside WSL:
-
wcmd
:通过cmd.exe
调用Win执行程序. -
wrun
:与CreateProcess
同步调用win-executable,然后等待死亡(不使用cmd.exe
). -
wstart
:启动一个分离的(异步)命令(使用cmd.exe
).
wcmd
: call a win-executable throughcmd.exe
.wrun
: call a win-executable synchronously withCreateProcess
, and wait to die (not usingcmd.exe
).wstart
: launch a detached (asynchronously) command (with the use ofcmd.exe
).
要使用它们,您必须:
- 安装 cbwin :
- 新的
outbash.exe
将安装在常规Windows文件系统中(在%PATH%
中的某个位置),再加上 - WSL文件系统中的3个linux命令.
- 新的
- Install cbwin:
- a new
outbash.exe
will be installed in your regular Windows filesystem (somewhere in your%PATH%
), plus - the 3 linux-commands in the WSL filesystem.
- a new
提示::如果使用 wcmd
或 wrun
启动的可执行文件产生了任何子代,则这些子代将仅在可执行文件保持活动的时间内存活.
Tip: If the executable launched with wcmd
or wrun
spawns any children, these survive only for as long that executable remains alive.
换句话说,尝试使用 wcmd
启动 notepad.exe
无效,因为 notepad 将在使用后立即被杀死.已启动-在这种情况下,请同时使用 wrun
或(异步) wstart
.
In other words, trying to start notepad.exe
with wcmd
won't work, because notepad will be killed just after having been launched -- Use wrun
(synchronously) or wstart
(asynchronously) in this case.
这篇关于如何从WSL(Ubuntu)Bash运行Windows可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!