From c187779799ea0c7aad9d10b2b6699616ab6be4e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Cant=C3=BA?= Date: Tue, 10 May 2022 11:30:56 -0700 Subject: [PATCH 1/2] CreateSymbolicLink PInvoke retval must be marshalled as U1 --- src/installer/tests/TestUtils/SymbolicLinking.cs | 1 + .../Windows/Kernel32/Interop.CreateSymbolicLink.cs | 9 +-------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/installer/tests/TestUtils/SymbolicLinking.cs b/src/installer/tests/TestUtils/SymbolicLinking.cs index bf7dbf45df0c8a..50b476368fca6b 100644 --- a/src/installer/tests/TestUtils/SymbolicLinking.cs +++ b/src/installer/tests/TestUtils/SymbolicLinking.cs @@ -64,6 +64,7 @@ private enum SymbolicLinkFlag } [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.U1)] private static extern bool CreateSymbolicLink( string symbolicLinkName, string targetFileName, diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateSymbolicLink.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateSymbolicLink.cs index b6d79e4bdb44ac..dc2f5bea9e49b8 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateSymbolicLink.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateSymbolicLink.cs @@ -21,7 +21,7 @@ internal static partial class Kernel32 internal const int SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE = 0x2; [LibraryImport(Libraries.Kernel32, EntryPoint = "CreateSymbolicLinkW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] - [return: MarshalAs(UnmanagedType.Bool)] + [return: MarshalAs(UnmanagedType.U1)] private static partial bool CreateSymbolicLinkPrivate(string lpSymlinkFileName, string lpTargetFileName, int dwFlags); /// @@ -55,17 +55,10 @@ internal static void CreateSymbolicLink(string symlinkFileName, string targetFil bool success = CreateSymbolicLinkPrivate(symlinkFileName, targetFileName, flags); - int error; if (!success) { throw Win32Marshal.GetExceptionForLastWin32Error(originalPath); } - // In older versions we need to check GetLastWin32Error regardless of the return value of CreateSymbolicLink, - // e.g: if the user doesn't have enough privileges to create a symlink the method returns success which we can consider as a silent failure. - else if (!isAtLeastWin10Build14972 && (error = Marshal.GetLastWin32Error()) != 0) - { - throw Win32Marshal.GetExceptionForWin32Error(error, originalPath); - } } } } From 9f9b298499a3e2a287ae64d71a5f5c0e41659ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Cant=C3=BA?= Date: Tue, 10 May 2022 11:54:45 -0700 Subject: [PATCH 2/2] Fix redundant invocations of Environment.OSVersion.Version --- .../Interop/Windows/Kernel32/Interop.CreateSymbolicLink.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateSymbolicLink.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateSymbolicLink.cs index dc2f5bea9e49b8..4d820e84250625 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateSymbolicLink.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateSymbolicLink.cs @@ -39,9 +39,8 @@ internal static void CreateSymbolicLink(string symlinkFileName, string targetFil int flags = 0; - bool isAtLeastWin10Build14972 = - Environment.OSVersion.Version.Major == 10 && Environment.OSVersion.Version.Build >= 14972 || - Environment.OSVersion.Version.Major >= 11; + Version osVersion = Environment.OSVersion.Version; + bool isAtLeastWin10Build14972 = osVersion.Major >= 11 || osVersion.Major == 10 && osVersion.Build >= 14972; if (isAtLeastWin10Build14972) {