Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateSymbolicLink PInvoke retval must be marshalled as U1 #69150

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/installer/tests/TestUtils/SymbolicLinking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
Expand Down Expand Up @@ -55,17 +55,10 @@ internal static void CreateSymbolicLink(string symlinkFileName, string targetFil

jkotas marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
}
}