Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ internal static unsafe partial class Windows
{
private static bool s_WNetApiNotAvailable;

[LibraryImport("mpr.dll", EntryPoint = "WNetGetConnectionW")]
internal static partial int WNetGetConnection(ReadOnlySpan<ushort> localName, Span<ushort> remoteName, ref uint remoteNameLength);
[LibraryImport("mpr.dll", EntryPoint = "WNetGetConnectionW", StringMarshalling = StringMarshalling.Utf16)]
internal static partial int WNetGetConnection(ReadOnlySpan<char> localName, Span<char> remoteName, ref uint remoteNameLength);

internal static int GetUNCForNetworkDrive(char drive, out string? uncPath)
{
Expand All @@ -33,11 +33,8 @@ internal static int GetUNCForNetworkDrive(char drive, out string? uncPath)
bufferSize = 3;
#endif

// TODO: change ushort with char after LibraryImport will support 'ref char'
// without applying the 'System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute'
// to the assembly.
ReadOnlySpan<ushort> driveName = stackalloc ushort[] { drive, ':', '\0' };
Span<ushort> uncBuffer = stackalloc ushort[(int)bufferSize];
ReadOnlySpan<char> driveName = stackalloc char[] { drive, ':', '\0' };
Span<char> uncBuffer = stackalloc char[(int)bufferSize];
int errorCode = ERROR_NO_NETWORK;

try
Expand All @@ -52,26 +49,28 @@ internal static int GetUNCForNetworkDrive(char drive, out string? uncPath)

if (errorCode == ERROR_SUCCESS)
{
uncPath = uncBuffer.Slice((int)bufferSize).ToString();
// exclude null terminator
uncPath = uncBuffer.Slice(0, (int)bufferSize - 1).ToString();
}
else if (errorCode == ERROR_MORE_DATA)
{
ushort[]? rentedArray = null;
char[]? rentedArray = null;
try
{
uncBuffer = rentedArray = ArrayPool<ushort>.Shared.Rent((int)bufferSize);
uncBuffer = rentedArray = ArrayPool<char>.Shared.Rent((int)bufferSize);
errorCode = WNetGetConnection(driveName, uncBuffer, ref bufferSize);

if (errorCode == ERROR_SUCCESS)
{
uncPath = uncBuffer.Slice((int)bufferSize).ToString();
// exclude null terminator
uncPath = uncBuffer.Slice(0, (int)bufferSize - 1).ToString();
}
}
finally
{
if (rentedArray is not null)
{
ArrayPool<ushort>.Shared.Return(rentedArray);
ArrayPool<char>.Shared.Return(rentedArray);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ Describe "Tests for New-PSDrive cmdlet." -Tag "CI","RequireAdminOnWindows" {
{ New-PSDrive -Name $PSDriveName -PSProvider FileSystem -Root $RemoteShare -Persist -ErrorAction Stop } | Should -Not -Throw
}

It "Network drive initialization on pwsh startup DisplayRoot should have value of share" -Skip:(-not $IsWindows) {
$null = New-PSDrive -Name $PSDriveName -PSProvider FileSystem -Root $RemoteShare -Persist -ErrorAction Stop
$drive = pwsh -noprofile -outputformat XML -command "Get-PSDrive -Name $PSDriveName"
$drive.DisplayRoot | Should -Be $RemoteShare.TrimEnd('\')
}

It "Should throw exception if root is not a remote share." -Skip:(-not $IsWindows) {
{ New-PSDrive -Name $PSDriveName -PSProvider FileSystem -Root "TestDrive:\" -Persist -ErrorAction Stop } | Should -Throw -ErrorId 'DriveRootNotNetworkPath'
}
Expand Down