Adapt to new behavior of nano server in resource updater (#42444)
authorVitek Karas <vitek.karas@microsoft.com>
Fri, 18 Sep 2020 15:24:14 +0000 (08:24 -0700)
committerGitHub <noreply@github.com>
Fri, 18 Sep 2020 15:24:14 +0000 (08:24 -0700)
In recent builds of nano server BeginUpdateResource will return ERROR_CALL_NOT_IMPLEMENTED. ResourceUpdater needs to adapt to provide a good error experience.

Without this change the code still fails, but with a much less friendly error.

src/installer/managed/Microsoft.NET.HostModel/ResourceUpdater.cs

index b0cc571..04b6b8a 100644 (file)
@@ -138,6 +138,8 @@ namespace Microsoft.NET.HostModel
             [DllImport(nameof(Kernel32), SetLastError=true)]
             public static extern uint SizeofResource(IntPtr hModule,
                                                      IntPtr hResInfo);
+
+            public const int ERROR_CALL_NOT_IMPLEMENTED = 0x78;
         }
 
         /// <summary>
@@ -183,9 +185,13 @@ namespace Microsoft.NET.HostModel
             {
                 // On Nano Server 1709+, `BeginUpdateResource` is exported but returns a null handle with a zero error
                 // Try to call `BeginUpdateResource` with an invalid parameter; the error should be non-zero if supported
+                // On Nano Server 20213, `BeginUpdateResource` fails with ERROR_CALL_NOT_IMPLEMENTED
                 using (var handle = Kernel32.BeginUpdateResource("", false))
                 {
-                    if (handle.IsInvalid && Marshal.GetLastWin32Error() == 0)
+                    int lastWin32Error = Marshal.GetLastWin32Error();
+
+                    if ((handle.IsInvalid && lastWin32Error == 0) ||
+                        lastWin32Error == Kernel32.ERROR_CALL_NOT_IMPLEMENTED)
                     {
                         return false;
                     }