Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log exception messages on .NET 6+ when dynamic library loading fails #204

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions source/icu.net/NativeMethods/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,16 @@ private static IntPtr GetIcuLibHandle(string basename, int icuVersion)
var libPath = string.IsNullOrEmpty(_IcuPath) ? libName : Path.Combine(_IcuPath, libName);

#if NET6_0_OR_GREATER
string exceptionErrorMessage = null;
loadMethod = "NativeLibrary.Load";
try
{
handle = NativeLibrary.Load(libPath);
}
catch (DllNotFoundException)
catch (DllNotFoundException ex)
{
handle = IntPtr.Zero;
exceptionErrorMessage = ex.Message;
}
#else
if (IsWindows)
Expand Down Expand Up @@ -385,14 +387,16 @@ private static IntPtr GetIcuLibHandle(string basename, int icuVersion)

lastError = Marshal.GetLastWin32Error();
#if NET6_0_OR_GREATER
if (!string.IsNullOrEmpty(exceptionErrorMessage))
exceptionErrorMessage = $" ({exceptionErrorMessage})";
var errorMsg = IsWindows
? new Win32Exception(lastError).Message
: $"{lastError}";
? $"{new Win32Exception(lastError).Message}{exceptionErrorMessage}"
: $"{lastError}({exceptionErrorMessage})";
#else
var errorMsg = IsWindows
? new Win32Exception(lastError).Message
: IsMac
? $"{lastError}"
? $"{lastError} (macOS loading requires .NET 6 or greater)"
: $"{lastError} ({dlerror()})";
#endif
Trace.WriteLineIf(lastError != 0, $"Unable to load [{libPath}]. Error: {errorMsg}");
Expand Down
Loading