[csharp] Fix error loading library grpc_csharp_ext.*.dll on windows with non-ASCII encoding (#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 <jtattermusch@google.com>
pull/27245/head
Alexey Mikheyev 4 years ago committed by GitHub
parent 6c095c957b
commit 4a267667b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      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")]

Loading…
Cancel
Save