diff --git a/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs b/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs index 577b11a44d6..c275559dfd1 100644 --- a/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs +++ b/src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs @@ -134,9 +134,19 @@ namespace Grpc.Core.Internal { if (PlatformApis.IsWindows) { - // TODO(jtattermusch): populate the error on Windows errorMsg = null; - return Windows.LoadLibrary(libraryPath); + var handle = Windows.LoadLibrary(libraryPath); + if (handle == IntPtr.Zero) + { + int win32Error = Marshal.GetLastWin32Error(); + errorMsg = $"LoadLibrary failed with error {win32Error}"; + // add extra info for the most common error ERROR_MOD_NOT_FOUND + if (win32Error == 126) + { + errorMsg += ": The specified module could not be found."; + } + } + return handle; } if (PlatformApis.IsLinux) { @@ -179,13 +189,13 @@ namespace Grpc.Core.Internal } } throw new FileNotFoundException( - String.Format("Error loading native library. Not found in any of the possible locations: {0}", + String.Format("Error loading native library. Not found in any of the possible locations: {0}", string.Join(",", libraryPathAlternatives))); } private static class Windows { - [DllImport("kernel32.dll")] + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] internal static extern IntPtr LoadLibrary(string filename); [DllImport("kernel32.dll")]