Reputation: 540
ever since they were first introduced, creating a symbolic link required a full administrator. Running from a normal or from a non-elevated process CreateSymbolicLink would fail.
In windows 10, CreateSymbolicLink fails as well in these circumstances, that is it doesn't create anything, however it returns a success code (!) and GetLastError is 0 too. So there's no way to detect the error other than checking if the symlink file exists
Looks like a bug in windows 10?
Upvotes: 4
Views: 3251
Reputation: 1479
This workarround works for me:
Change the return value into an integer.
1 = success
for all other values call GetLastWin32Error
[DllImport("kernel32.dll", EntryPoint = "CreateSymbolicLinkW", CharSet = CharSet.Unicode, SetLastError = true)]
static extern int CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, SYMBOLIC_LINK_FLAG dwFlags);
public static int CreateSymbolicLinkFix(string lpSymlinkFileName, string lpTargetFileName, int dwFlags) {
var result = CreateSymbolicLink(lpSymlinkFileName, lpTargetFileName, dwFlags);
if (result == 1) return 0; // Success
return Marshal.GetLastWin32Error();
}
Upvotes: 1
Reputation: 11
Have experienced the same. But: The success code you seem to get is an error code. So it seems the have changed the return type of CreateSymbolicLink from BOOLEAN to int
Upvotes: 1