运行 来自 C++ 代码的 WSL 命令超过 windows

Running WSL command from C++ code over windows

我在 windows 中 运行ning c++ 代码,我想从中 运行 wsl 上的命令。

命令如下:

ShellExecuteA(NULL, "open", "cmd", "bash -c \"rm -f /tmp/xyz.log\"" , NULL, SW_SHOW);

并且

system("start \"bash -c \"rm -f /tmp/xyz.log\"\"");

以上两种方法我都试过了,但都不行。尽管这些命令在 WSL 命令提示符下有效。

我试图将这个和 运行 立即复制到这个问题中。在(相当)有点混乱之后,我发现 this link 并意识到我的测试项目是针对 x86 而不是 x64 。呃...

wsl.exebash.exe为64位文件,存放在c:\Windows\System32中。如果您尝试从 x86 应用程序 运行ning 它们,它们似乎丢失了。这是因为 Windows 向 32 位应用程序显示 不同的 、32 位特定 c:\Windows\System32 文件夹。 64 位 System32 文件夹将出现在 C:\Windows\Sysnative.

简单的解决方案是将目标更改为 x64。一旦完成,甚至

system("wsl ls -la");

system("bash -c ls -la");

只是工作。

对于 x86 应用程序,解决方案是使用 Sysnative 中的绝对路径,例如:

system("c:/windows/sysnative/bash -c ls -la");

system("c:/windows/sysnative/wsl -c ls -la");