Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run WSL command without opening CMD Window #123

Open
pclass-isolated opened this issue Oct 3, 2024 · 1 comment
Open

How to run WSL command without opening CMD Window #123

pclass-isolated opened this issue Oct 3, 2024 · 1 comment

Comments

@pclass-isolated
Copy link

How can I hide the CMD windows that opens when I run a WSL command? I tried the following, but the command object is missing the attribute for hidden window flag. My goal is to run a WSL command without opening a CMD window on Windows 11. Thanks.

// Get the command
cmd := distro.Command(context.Background(), command)

// Ensure to hide the window
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} // This fails to compile

// Run wsl command and get output
out, err := cmd.CombinedOutput()
if err != nil {
    fmt.Fprintf(os.Stderr, "Unexpected error: %v\nError message: %s", err, out)
    return errors.New("Error: Failed to run command: " + fmt.Sprintf("Unexpected error: %v\nError message: %s", err, out))
} else {
    pp.Println("WSL: " + string(out))
    return nil
}
@CarlosNihelton
Copy link
Collaborator

Hi! We don't handle that possibility in this library because it either subprocess wsl.exe or run functions from the wslapi.dll, both require the parent process to be a console process otherwise they create a console for their own.

There is a quite involving alternative of creating a pseudoconsole to host the spawned process. One way to leverage that is writing your main application as a console application and having a Windows application (GUI subsystem) to create a pseudoconsole and host your main application. You control the pseudoconsole, its contents will only be displayed if you make the effort to do so. If you make your GUI invisible then everything will be invisible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants