Skip to content

Commit

Permalink
Fix to ensure console window is hidden on Windows 11 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-pro authored Apr 12, 2023
1 parent d5d7537 commit b533afe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wsl2-dns-agent"
version = "0.3.4"
version = "0.3.5"
edition = "2021"
license = "GPL-3.0"
description = "An agent that automatically patches your WSL2 DNS configuration for users of Cisco AnyConnect (or similar VPNs)"
Expand All @@ -18,7 +18,7 @@ serde = { version = "1.0.137", features = ["derive"] }
simplelog = "0.12.0"
thiserror = "1.0.31"
toml = "0.5.9"
win32-utils = { git = "https://github.com/jacob-pro/win32-utils", features = ["net", "window", "instance", "console"] }
win32-utils = { git = "https://github.com/jacob-pro/win32-utils", features = ["net", "window", "instance"] }

[dependencies.windows]
version = "0.37.0"
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![windows_subsystem = "windows"]
use crate::runner::{start_runner, RunReason};
use crate::tray::Tray;
use log::LevelFilter;
Expand All @@ -6,7 +7,6 @@ use std::ffi::c_void;
use std::fs;
use std::fs::File;
use std::sync::mpsc::Sender;
use win32_utils::console::hide_console_window_if_in_process;
use win32_utils::instance::UniqueInstance;
use win32_utils::str::ToWin32Str;
use windows::core::PCWSTR;
Expand All @@ -27,7 +27,6 @@ pub const APP_NAME: &str = "WSL2 DNS Agent";

fn main() {
set_panic();
hide_console_window_if_in_process();

let _unique = match UniqueInstance::acquire_unique_to_session(APP_NAME) {
Ok(u) => u,
Expand Down
7 changes: 7 additions & 0 deletions src/wsl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::io::Write;
use std::os::windows::process::CommandExt;
use std::process::{Command, Output, Stdio};
use std::string::{FromUtf16Error, FromUtf8Error};
use thiserror::Error;
use windows::Win32::System::Threading::CREATE_NO_WINDOW;

#[derive(Debug)]
pub struct WslDistribution {
Expand Down Expand Up @@ -51,6 +53,7 @@ fn to_u16(original: &[u8]) -> Vec<u16> {

pub fn get_distributions() -> Result<Vec<WslDistribution>, Error> {
let output = Command::new("wsl.exe")
.creation_flags(CREATE_NO_WINDOW.0)
.arg("--list")
.arg("--verbose")
.output()?;
Expand Down Expand Up @@ -101,6 +104,7 @@ fn check_wsl_output(output: &Output) -> Result<(), Error> {
impl WslDistribution {
pub fn read_file(&self, path: &str) -> Result<String, Error> {
let output = Command::new("wsl.exe")
.creation_flags(CREATE_NO_WINDOW.0)
.arg("--distribution")
.arg(&self.name)
.arg("--user")
Expand All @@ -115,6 +119,7 @@ impl WslDistribution {
pub fn set_read_only(&self, path: &str, read_only: bool) -> Result<(), Error> {
let arg = if read_only { "+i" } else { "-i" };
let output = Command::new("wsl.exe")
.creation_flags(CREATE_NO_WINDOW.0)
.arg("--distribution")
.arg(&self.name)
.arg("--user")
Expand All @@ -129,6 +134,7 @@ impl WslDistribution {

pub fn write_file(&self, path: &str, contents: &str) -> Result<(), Error> {
let mut p = Command::new("wsl.exe")
.creation_flags(CREATE_NO_WINDOW.0)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
Expand All @@ -151,6 +157,7 @@ impl WslDistribution {

pub fn terminate(&self) -> Result<(), Error> {
let output = Command::new("wsl.exe")
.creation_flags(CREATE_NO_WINDOW.0)
.arg("--terminate")
.arg(&self.name)
.output()?;
Expand Down

0 comments on commit b533afe

Please sign in to comment.