1

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.

12
  • You've bash in Windows? Why are you not using del command?
    – Azeem
    Commented Oct 5, 2018 at 8:01
  • I have a specific requirement of running the shell script from the C++ code running on windows. So that required me to run this over the cmd prompt using the WSL, I am trying to do the same Commented Oct 5, 2018 at 8:04
  • I would expect there to be a way to run through wsl.exe (like in a Windows prompt) but you probably need to know more about Windows than I do to get that working...
    – molbdnilo
    Commented Oct 5, 2018 at 8:15
  • @user1867087: Right. Your wsl.exe paths are set, right?
    – Azeem
    Commented Oct 5, 2018 at 8:22
  • @user1867087: Did you try wsl args... or cmd wsl arg...?
    – Azeem
    Commented Oct 5, 2018 at 8:55

1 Answer 1

1

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");

Not the answer you're looking for? Browse other questions tagged or ask your own question.