Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/Interop/Windows/WNetGetConnection.cs (C#)

Code before:
using System;
using System.Buffers;
using System.Runtime.InteropServices;

internal static partial class Interop
{
    internal static unsafe partial class Windows
    {
        private static bool s_WNetApiNotAvailable;

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

        internal static int GetUNCForNetworkDrive(char drive, out string? uncPath)
        {
            uncPath = null;
            if (s_WNetApiNotAvailable)
            {
                return ERROR_NOT_SUPPORTED;
            }

#if DEBUG
            int testBufferSize = System.Management.Automation.Internal.InternalTestHooks.WNetGetConnectionBufferSize;
            int bufferSize = testBufferSize == 0 ? MAX_PATH : testBufferSize;
#else
            int bufferSize = MAX_PATH;
#endif

            ReadOnlySpan<char> driveName = stackalloc char[] { drive, ':', '\0' };
            Span<char> uncBuffer = stackalloc char[bufferSize];

            char[]? rentedArray = null;
            while (true)
            {
                int errorCode;
                try
                {
                    try
                    {
                        errorCode = WNetGetConnection(driveName, uncBuffer, ref bufferSize);
                    }
                    catch (DllNotFoundException)
                    {
                        s_WNetApiNotAvailable = true;
                        return ERROR_NOT_SUPPORTED;
                    }

                    if (errorCode == ERROR_SUCCESS)
                    {
                        // Cannot rely on bufferSize as it's only set if
                        // the first call ended with ERROR_MORE_DATA,

Review comment (bug):
Since CI run tests only in Release config we should remove the `#if DEBUG`.
Also best practice is to allocate stackalloc with const and then slice if needed.

```c#
internal static int WNetGetConnectionBufferSize = -1; // Including terminate '\0'

...

int bufferSize = MAX_PATH;
Span<char> uncBuffer = stackalloc char[bufferSize];
if (InternalTestHooks.WNetGetConnectionBufferSize > 0)
{
    bufferSize = InternalTestHooks.WNetGetConnectionBufferSize;
    uncBuffer = uncBuffer.Slice(0, bufferSize);
}
```


Code after:
using System;
using System.Buffers;
using System.Runtime.InteropServices;
using System.Management.Automation.Internal;

internal static partial class Interop
{
    internal static unsafe partial class Windows
    {
        private static bool s_WNetApiNotAvailable;

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

        internal static int GetUNCForNetworkDrive(char drive, out string? uncPath)
        {
            uncPath = null;
            if (s_WNetApiNotAvailable)
            {
                return ERROR_NOT_SUPPORTED;
            }

            ReadOnlySpan<char> driveName = stackalloc char[] { drive, ':', '\0' };
            int bufferSize = MAX_PATH;
            Span<char> uncBuffer = stackalloc char[MAX_PATH];
            if (InternalTestHooks.WNetGetConnectionBufferSize > 0 && InternalTestHooks.WNetGetConnectionBufferSize <= MAX_PATH)
            {
                bufferSize = InternalTestHooks.WNetGetConnectionBufferSize;
                uncBuffer = uncBuffer.Slice(0, bufferSize);
            }

            char[]? rentedArray = null;
            while (true)
            {
                int errorCode;
                try
                {
                    try
                    {
                        errorCode = WNetGetConnection(driveName, uncBuffer, ref bufferSize);
                    }
                    catch (DllNotFoundException)
                    {
                        s_WNetApiNotAvailable = true;
                        return ERROR_NOT_SUPPORTED;
                    }

                    if (errorCode == ERROR_SUCCESS)
                    {
                        // Cannot rely on bufferSize as it's only set if
                        // the first call ended with ERROR_MORE_DATA,