From 48a65e05bf9e56364d5538f5f4ae7646d6316c3f Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Tue, 30 Jul 2024 10:23:49 +0900 Subject: [PATCH] Fix failure to create GrpcChannel under Wine compatibility layer --- src/Grpc.Net.Client/Internal/Native.cs | 37 ++++++++++++++++---------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/Grpc.Net.Client/Internal/Native.cs b/src/Grpc.Net.Client/Internal/Native.cs index c113030d9..90dd74fb1 100644 --- a/src/Grpc.Net.Client/Internal/Native.cs +++ b/src/Grpc.Net.Client/Internal/Native.cs @@ -64,21 +64,30 @@ internal static bool IsUwp(string frameworkDescription, Version version) } else { - var bufferSize = 0U; - var result = GetCurrentApplicationUserModelId(ref bufferSize, Array.Empty()); - switch (result) + try { - case 15703: // APPMODEL_ERROR_NO_APPLICATION - return false; - case 0: // ERROR_SUCCESS - case 122: // ERROR_INSUFFICIENT_BUFFER - // Success is actually insufficient buffer as we're really only looking for - // not NO_APPLICATION and we're not actually giving a buffer here. The - // API will always return NO_APPLICATION if we're not running under a - // WinRT process, no matter what size the buffer is. - return true; - default: - throw new InvalidOperationException($"Failed to get AppModelId, result was {result}."); + var bufferSize = 0U; + var result = GetCurrentApplicationUserModelId(ref bufferSize, Array.Empty()); + switch (result) + { + case 15703: // APPMODEL_ERROR_NO_APPLICATION + return false; + case 0: // ERROR_SUCCESS + case 122: // ERROR_INSUFFICIENT_BUFFER + // Success is actually insufficient buffer as we're really only looking for + // not NO_APPLICATION and we're not actually giving a buffer here. The + // API will always return NO_APPLICATION if we're not running under a + // WinRT process, no matter what size the buffer is. + return true; + default: + throw new InvalidOperationException($"Failed to get AppModelId, result was {result}."); + } + } + catch (EntryPointNotFoundException) + { + // Wine compatibility layers such as Steam Deck/Steam OS Proton and the Apple Game Porting Toolkit + // return Windows 8 or later as the OS version, but does not implement the GetCurrentApplicationUserModelId API. + return false; } } }