user1867087
user1867087

Reputation: 63

Running WSL command from C++ code over windows

I am running c++ code in windows from which I want to run the commands on the wsl.

Here are the commands:

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

and,

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

I tried both of the above but it doesn't work. Although these commands work on the WSL command prompt.

Upvotes: 1

Views: 1366

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131704

I tried to replicate this and run into this problem immediately. After (quite) a bit of confusion, I found this link and realized my test project was targeting x86 instead of x64 . Duh ...

wsl.exe and bash.exe are 64-bit files, stored in c:\Windows\System32. If you try running them from an x86 application they appear to be missing. That's because Windows shows a different, 32-bit specific c:\Windows\System32 folder to 32-bit applications. The 64-bit System32 folder will appear under C:\Windows\Sysnative.

The easy solution is to change the target to x64. Once that's done, even

system("wsl ls -la");

or

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

Just work.

For x86 applications the solution is to use the absolute path in Sysnative, eg :

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

or

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

Upvotes: 1

Related Questions