From 62879d87c59eb0f324f2ce458f6eb6206e154bfd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 May 2025 08:31:45 +0000 Subject: [PATCH 1/3] Initial plan for issue From b52bc1109b82e0b3c8c72085f7172deadfd505a4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 May 2025 08:35:06 +0000 Subject: [PATCH 2/3] Add try-catch for Environment.SetEnvironmentVariable in UndockedRegFreeWinRT Co-authored-by: haonanttt <186026394+haonanttt@users.noreply.github.com> --- BuildAll.ps1 | 0 .../UndockedRegFreeWinRT-AutoInitializer.cs | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) mode change 100644 => 100755 BuildAll.ps1 diff --git a/BuildAll.ps1 b/BuildAll.ps1 old mode 100644 new mode 100755 diff --git a/dev/UndockedRegFreeWinRT/UndockedRegFreeWinRT-AutoInitializer.cs b/dev/UndockedRegFreeWinRT/UndockedRegFreeWinRT-AutoInitializer.cs index 2e07c542..7b6e0dac 100644 --- a/dev/UndockedRegFreeWinRT/UndockedRegFreeWinRT-AutoInitializer.cs +++ b/dev/UndockedRegFreeWinRT/UndockedRegFreeWinRT-AutoInitializer.cs @@ -24,7 +24,20 @@ class AutoInitialize internal static void AccessWindowsAppSDK() { // Set base directory env var for PublishSingleFile support (referenced by SxS redirection) - Environment.SetEnvironmentVariable("MICROSOFT_WINDOWSAPPRUNTIME_BASE_DIRECTORY", AppContext.BaseDirectory); + try + { + Environment.SetEnvironmentVariable("MICROSOFT_WINDOWSAPPRUNTIME_BASE_DIRECTORY", AppContext.BaseDirectory); + } + catch (System.MissingMethodException) + { + // Gracefully handle the case when Environment.SetEnvironmentVariable is not available + // This can happen in trimmed environments (PublishTrimmed=true) + // Continue execution as it's better than crashing the application + } + catch (System.Exception) + { + // Catch any other unexpected exceptions to ensure application doesn't crash + } // No error handling needed as the target function does nothing (just {return S_OK}). // It's the act of calling the function causing the DllImport to load the DLL that From ceb2aefad899a0847f84b0133521410bb8dc1f1c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 May 2025 08:35:52 +0000 Subject: [PATCH 3/3] Add Win32 SetEnvironmentVariable fallback in UndockedRegFreeWinRT Co-authored-by: haonanttt <186026394+haonanttt@users.noreply.github.com> --- .../UndockedRegFreeWinRT-AutoInitializer.cs | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/dev/UndockedRegFreeWinRT/UndockedRegFreeWinRT-AutoInitializer.cs b/dev/UndockedRegFreeWinRT/UndockedRegFreeWinRT-AutoInitializer.cs index 7b6e0dac..70d0c100 100644 --- a/dev/UndockedRegFreeWinRT/UndockedRegFreeWinRT-AutoInitializer.cs +++ b/dev/UndockedRegFreeWinRT/UndockedRegFreeWinRT-AutoInitializer.cs @@ -16,6 +16,11 @@ internal static class NativeMethods { [DllImport("Microsoft.WindowsAppRuntime.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] internal static extern int WindowsAppRuntime_EnsureIsLoaded(); + + // Direct Win32 API call to set environment variable as fallback + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool SetEnvironmentVariableW(string name, string value); } class AutoInitialize @@ -30,13 +35,30 @@ internal static void AccessWindowsAppSDK() } catch (System.MissingMethodException) { - // Gracefully handle the case when Environment.SetEnvironmentVariable is not available + // Fallback: Use direct Win32 API call when Environment.SetEnvironmentVariable is not available // This can happen in trimmed environments (PublishTrimmed=true) - // Continue execution as it's better than crashing the application + try + { + NativeMethods.SetEnvironmentVariableW("MICROSOFT_WINDOWSAPPRUNTIME_BASE_DIRECTORY", AppContext.BaseDirectory); + } + catch + { + // Ignore failures in the fallback path - continue execution + // as it's better than crashing the application + } } catch (System.Exception) { // Catch any other unexpected exceptions to ensure application doesn't crash + // Try the fallback mechanism + try + { + NativeMethods.SetEnvironmentVariableW("MICROSOFT_WINDOWSAPPRUNTIME_BASE_DIRECTORY", AppContext.BaseDirectory); + } + catch + { + // Ignore failures in the fallback path + } } // No error handling needed as the target function does nothing (just {return S_OK}).