Skip to content

Commit

Permalink
[csharp] Fix error loading library grpc_csharp_ext.*.dll on windows w…
Browse files Browse the repository at this point in the history
…ith non-ASCII encoding (grpc#26762)

* [csharp net472] Fix error loading library grpc_csharp_ext.x64.dll on Windows with charset on non ANSI encoding. Example library path: C:\Program Files (x86)\тест\grpc_csharp_ext.x64.dll

* cleanup

* Change charset to Unicode

Co-authored-by: Jan Tattermusch <[email protected]>
  • Loading branch information
trs4 and jtattermusch authored Sep 3, 2021
1 parent 6c095c9 commit 4a26766
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/csharp/Grpc.Core/Internal/UnmanagedLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,19 @@ private static IntPtr PlatformSpecificLoadLibrary(string libraryPath, out string
{
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)
{
Expand Down Expand Up @@ -179,13 +189,13 @@ private static string FirstValidLibraryPath(string[] libraryPathAlternatives)
}
}
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")]
Expand Down

0 comments on commit 4a26766

Please sign in to comment.