/
githubmirror
/
PowerShell
Обзор
Документация
Войти
/
githubmirror
/
PowerShell
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/System.Management.Automation/engine/Interop/Windows/WNetGetConnection.cs
86 строк
3 KB
xtqqczze
Replace stackallocs with collection expressions (#25803)
17 окт 2025, 07:38
Не верифицирован
17 окт 2025, 07:38
b4f42a0
Код
Авторство
О чём код?
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. #nullable enable 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 = [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, // instead slice at the null terminator. unsafe { fixed (char* uncBufferPtr = uncBuffer) { uncPath = new string(uncBufferPtr); } } } } finally { if (rentedArray is not null) { ArrayPool<char>.Shared.Return(rentedArray); } } if (errorCode == ERROR_MORE_DATA) { uncBuffer = rentedArray = ArrayPool<char>.Shared.Rent(bufferSize); } else { return errorCode; } } } } }