如何从WSL(Ubuntu)Bash运行Windows可执行文件 [英] How can a run Windows executable from WSL (Ubuntu) Bash

查看:1098
本文介绍了如何从WSL(Ubuntu)Bash运行Windows可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着2016年夏季的 Windows 10周年更新,可以在新的ubuntu 二进制文件. microsoft.com/wsl/2016/04/22/windows-subsystem-for-linux-overview/>适用于Linux的Windows子系统(WSL),是轻型"虚拟子系统.

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 through cmd.exe.
  • wrun: call a win-executable synchronously with CreateProcess, and wait to die (not using cmd.exe).
  • wstart: launch a detached (asynchronously) command (with the use of cmd.exe).

要使用它们,您必须:

  1. 安装 cbwin :
    • 新的outbash.exe将安装在常规Windows文件系统(%PATH%中的某个位置)中,以及
    • WSL文件系统中的3个linux命令.
  1. 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.

提示:如果使用wcmdwrun启动的可执行文件产生了任何子代,则这些子代将仅在该可执行文件保持活动状态的情况下存活.

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 将在启动后立即被杀死-同步使用wrunwstart(异步)在这种情况下.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆