Skip to content

Commit

Permalink
Use registry for VMID
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Sep 9, 2021
1 parent 629efbf commit 7096d40
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tokio = { version = "1.7", features = ["net", "rt", "macros", "io-util", "time"]
async-io = "1"
once_cell = "1"
winapi = { version = "0.3", features = ["wincon", "libloaderapi", "combaseapi"] }
winreg = "0.9"
widestring = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
Expand Down
30 changes: 27 additions & 3 deletions server/src/vmcompute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use uuid::Uuid;

#[serde(rename_all = "PascalCase")]
#[derive(Debug, Deserialize)]
pub struct ComputeSystem {
struct ComputeSystem {
pub id: Uuid,
pub system_type: String,
pub owner: String,
Expand All @@ -12,7 +12,7 @@ pub struct ComputeSystem {
pub state: String,
}

pub fn enumerate_compute_systems(query: &str) -> std::io::Result<Vec<ComputeSystem>> {
fn enumerate_compute_systems(query: &str) -> std::io::Result<Vec<ComputeSystem>> {
use std::ffi::CString;
use std::io::{Error, ErrorKind};
use widestring::WideCString;
Expand Down Expand Up @@ -73,7 +73,8 @@ pub fn enumerate_compute_systems(query: &str) -> std::io::Result<Vec<ComputeSyst
}
}

pub fn get_wsl_vmid() -> std::io::Result<Option<Uuid>> {
#[allow(unused)]
fn get_wsl_vmid_by_hcs() -> std::io::Result<Option<Uuid>> {
let vms = enumerate_compute_systems("{}")?;
for vm in vms {
if vm.owner == "WSL" {
Expand All @@ -82,3 +83,26 @@ pub fn get_wsl_vmid() -> std::io::Result<Option<Uuid>> {
}
Ok(None)
}

pub fn get_wsl_vmid_by_reg() -> std::io::Result<Option<Uuid>> {
let list = winreg::RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE)
.open_subkey(r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\HostComputeService\VolatileStore\ComputeSystem")?;
for k in list.enum_keys() {
let k = k?;
let subkey = list.open_subkey(&k)?;
let ty: u32 = match subkey.get_value("ComputeSystemType") {
Ok(v) => v,
Err(_) => continue,
};
if ty == 2 {
if let Ok(v) = k.parse() {
return Ok(Some(v));
}
}
}
Ok(None)
}

pub fn get_wsl_vmid() -> std::io::Result<Option<Uuid>> {
get_wsl_vmid_by_reg()
}

0 comments on commit 7096d40

Please sign in to comment.