diff --git a/crates/punktfunk-host/src/capture/wgc_relay.rs b/crates/punktfunk-host/src/capture/wgc_relay.rs index df5145e..54b2a37 100644 --- a/crates/punktfunk-host/src/capture/wgc_relay.rs +++ b/crates/punktfunk-host/src/capture/wgc_relay.rs @@ -152,6 +152,43 @@ unsafe fn no_inherit(h: HANDLE) { let _ = SetHandleInformation(h, HANDLE_FLAG_INHERIT.0, HANDLE_FLAGS(0)); } +/// Build the helper's environment block: the user's block (so DLL/PATH/SystemRoot resolve) with this +/// (host) process's `PUNKTFUNK_*` vars overlaid, so the helper encodes with the SAME settings the +/// host runs with (`PUNKTFUNK_ENCODER=nvenc`, `PUNKTFUNK_ZEROCOPY`, …) instead of the user shell's. +/// Returns a UTF-16, double-null-terminated block suitable for `CREATE_UNICODE_ENVIRONMENT`. +unsafe fn merged_env_block(user_block: *const u16) -> Vec { + // Parse the user block ("VAR=VALUE\0" … "\0") into entries. + let mut entries: Vec = Vec::new(); + if !user_block.is_null() { + let mut p = user_block; + loop { + let mut len = 0isize; + while *p.offset(len) != 0 { + len += 1; + } + if len == 0 { + break; // the trailing empty string = end of block + } + let slice = std::slice::from_raw_parts(p, len as usize); + entries.push(String::from_utf16_lossy(slice)); + p = p.offset(len + 1); + } + } + // Drop any PUNKTFUNK_* the user block carried, then overlay this process's PUNKTFUNK_* vars. + entries.retain(|e| !e.split('=').next().unwrap_or("").starts_with("PUNKTFUNK_")); + for (k, v) in std::env::vars().filter(|(k, _)| k.starts_with("PUNKTFUNK_")) { + entries.push(format!("{k}={v}")); + } + // Serialize back to a UTF-16 double-null-terminated block. + let mut block: Vec = Vec::new(); + for e in entries { + block.extend(e.encode_utf16()); + block.push(0); + } + block.push(0); + block +} + unsafe fn spawn_inner(cmdline: &str, w: u32, h: u32, hz: u32) -> Result { // The user token of the active console session (requires the host to be SYSTEM). let session = WTSGetActiveConsoleSessionId(); @@ -175,9 +212,17 @@ unsafe fn spawn_inner(cmdline: &str, w: u32, h: u32, hz: u32) -> Result Result Result