diff --git a/SpecProbe.Loader/EnvironmentTools.cs b/SpecProbe.Loader/EnvironmentTools.cs index ff8a195..9fde89f 100644 --- a/SpecProbe.Loader/EnvironmentTools.cs +++ b/SpecProbe.Loader/EnvironmentTools.cs @@ -65,7 +65,7 @@ public static string GetEnvironmentVariableUcrt(string variable) IntPtr varNamePtr = Marshal.StringToHGlobalAnsi(variable); int result = getenv_s(ref size, IntPtr.Zero, 0, varNamePtr); if (result != 0) - throw new Exception($"Environment {variable} can't be get."); + throw new Exception($"Environment {variable} can't be get. [0x{Marshal.GetLastWin32Error():X8}]"); // Check the size if (size == 0) @@ -75,7 +75,7 @@ public static string GetEnvironmentVariableUcrt(string variable) IntPtr buffer = Marshal.AllocHGlobal(size * sizeof(char)); result = getenv_s(ref size, buffer, size, varNamePtr); if (result != 0) - throw new Exception($"Environment {variable} can't be get with buffer size {size}."); + throw new Exception($"Environment {variable} can't be get with buffer size {size}. [0x{Marshal.GetLastWin32Error():X8}]"); // Convert the value to a string string value = Marshal.PtrToStringAnsi(buffer); @@ -136,7 +136,7 @@ public static void SetEnvironmentVariableUcrt(string variable, string value) { int result = _putenv_s(variable, value); if (result != 0) - throw new Exception($"Environment {variable} can't be set to {value}."); + throw new Exception($"Environment {variable} can't be set to {value}. [0x{Marshal.GetLastWin32Error():X8}]"); } /// @@ -149,7 +149,7 @@ public static void SetEnvironmentVariableAppendUcrt(string variable, string valu string oldValue = GetEnvironmentVariableUcrt(variable); int result = _putenv_s(variable, oldValue + value); if (result != 0) - throw new Exception($"Environment {variable} can't be set to {value}."); + throw new Exception($"Environment {variable} can't be set to {value}. [0x{Marshal.GetLastWin32Error():X8}]"); } /// @@ -164,7 +164,7 @@ public static void SetEnvironmentVariableNoOverwriteUcrt(string variable, string { int result = _putenv_s(variable, value); if (result != 0) - throw new Exception($"Environment {variable} can't be set to {value}."); + throw new Exception($"Environment {variable} can't be set to {value}. [0x{Marshal.GetLastWin32Error():X8}]"); } } @@ -177,7 +177,7 @@ public static void SetEnvironmentVariableLibc(string variable, string value) { int result = setenv(variable, value, 1); if (result != 0) - throw new Exception($"Environment {variable} can't be set to {value}."); + throw new Exception($"Environment {variable} can't be set to {value}. [0x{Marshal.GetLastWin32Error():X8}]"); } /// @@ -190,7 +190,7 @@ public static void SetEnvironmentVariableAppendLibc(string variable, string valu string oldValue = GetEnvironmentVariableLibc(variable); int result = setenv(variable, oldValue + value, 1); if (result != 0) - throw new Exception($"Environment {variable} can't be set to {value}."); + throw new Exception($"Environment {variable} can't be set to {value}. [0x{Marshal.GetLastWin32Error():X8}]"); } /// @@ -203,23 +203,23 @@ public static void SetEnvironmentVariableNoOverwriteLibc(string variable, string string oldValue = GetEnvironmentVariableLibc(variable); int result = setenv(variable, value, 0); if (result != 0) - throw new Exception($"Environment {variable} can't be set to {value}."); + throw new Exception($"Environment {variable} can't be set to {value}. [0x{Marshal.GetLastWin32Error():X8}]"); } #region Interop #region Windows - [DllImport("UCRTBASE.DLL")] + [DllImport("UCRTBASE.DLL", SetLastError = true)] internal static extern int getenv_s(ref int requiredSize, IntPtr buffer, int bufferSize, IntPtr varname); - [DllImport("UCRTBASE.DLL")] + [DllImport("UCRTBASE.DLL", SetLastError = true)] internal static extern int _putenv_s(string e, string v); #endregion #region Unix - [DllImport("libc", CharSet = CharSet.Ansi)] + [DllImport("libc", CharSet = CharSet.Ansi, SetLastError = true)] internal static extern IntPtr getenv(string name); - [DllImport("libc", CharSet = CharSet.Ansi)] + [DllImport("libc", CharSet = CharSet.Ansi, SetLastError = true)] internal static extern int setenv(string name, string value, int overwrite); #endregion #endregion diff --git a/SpecProbe.Loader/LibraryFile.cs b/SpecProbe.Loader/LibraryFile.cs index 2f12822..7804aaf 100644 --- a/SpecProbe.Loader/LibraryFile.cs +++ b/SpecProbe.Loader/LibraryFile.cs @@ -50,7 +50,7 @@ internal void LoadItem() else throw new PlatformNotSupportedException("Unsupported platform."); if (handle == IntPtr.Zero) - throw new InvalidOperationException("This library or one of its dependencies failed to load: " + FilePath); + throw new InvalidOperationException($"This library or one of its dependencies failed to load: [0x{Marshal.GetLastWin32Error():X8}] " + FilePath); } internal IntPtr LoadSymbol(string symbolName) @@ -160,37 +160,37 @@ private IntPtr LoadMacOSLibrary(string path) private static class Windows { - [DllImport("kernel32.dll")] + [DllImport("kernel32.dll", SetLastError = true)] internal static extern IntPtr LoadLibrary(string filename); - [DllImport("kernel32.dll")] + [DllImport("kernel32.dll", SetLastError = true)] internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName); } private static class Linux { - [DllImport("libdl.so")] + [DllImport("libdl.so", SetLastError = true)] internal static extern IntPtr dlopen(string filename, int flags); - [DllImport("libdl.so")] + [DllImport("libdl.so", SetLastError = true)] internal static extern IntPtr dlsym(IntPtr handle, string symbol); - [DllImport("libdl.so.2", EntryPoint = "dlopen")] + [DllImport("libdl.so.2", EntryPoint = "dlopen", SetLastError = true)] internal static extern IntPtr dlopen_new(string filename, int flags); - [DllImport("libdl.so.2", EntryPoint = "dlsym")] + [DllImport("libdl.so.2", EntryPoint = "dlsym", SetLastError = true)] internal static extern IntPtr dlsym_new(IntPtr handle, string symbol); } private static class MacOSX { - [DllImport("libSystem.dylib")] + [DllImport("libSystem.dylib", SetLastError = true)] internal static extern IntPtr dlopen(string filename, int flags); - [DllImport("libSystem.dylib")] + [DllImport("libSystem.dylib", SetLastError = true)] internal static extern IntPtr dlsym(IntPtr handle, string symbol); } private static class Mono { - [DllImport("__Internal")] + [DllImport("__Internal", SetLastError = true)] internal static extern IntPtr dlopen(string filename, int flags); - [DllImport("__Internal")] + [DllImport("__Internal", SetLastError = true)] internal static extern IntPtr dlsym(IntPtr handle, string symbol); }