Skip to content

Commit

Permalink
add - Added error codes to native loader
Browse files Browse the repository at this point in the history
---

We've added error codes to the native loader exceptions.

---

Type: add
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Nov 14, 2024
1 parent 716f83f commit 91d3d41
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
24 changes: 12 additions & 12 deletions SpecProbe.Loader/EnvironmentTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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}]");
}

/// <summary>
Expand All @@ -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}]");
}

/// <summary>
Expand All @@ -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}]");
}
}

Expand All @@ -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}]");
}

/// <summary>
Expand All @@ -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}]");
}

/// <summary>
Expand All @@ -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
Expand Down
22 changes: 11 additions & 11 deletions SpecProbe.Loader/LibraryFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 91d3d41

Please sign in to comment.