More corelib cleanup (dotnet/coreclr#26993)
[mono-project.git] / netcore / System.Private.CoreLib / shared / Interop / Windows / Kernel32 / Interop.GetComputerName.cs
blob011430da5c7e85b5c6fb6beb841d0946accdfc69
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 #nullable enable
6 using System;
7 using System.Runtime.InteropServices;
9 internal static partial class Interop
11 internal static partial class Kernel32
13 [DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, EntryPoint = "GetComputerNameW")]
14 private static extern unsafe int GetComputerName(ref char lpBuffer, ref uint nSize);
16 // maximum length of the NETBIOS name (not including NULL)
17 private const int MAX_COMPUTERNAME_LENGTH = 15;
19 internal static unsafe string? GetComputerName()
21 Span<char> buffer = stackalloc char[MAX_COMPUTERNAME_LENGTH + 1];
22 uint length = (uint)buffer.Length;
24 return GetComputerName(ref MemoryMarshal.GetReference(buffer), ref length) != 0 ?
25 buffer.Slice(0, (int)length).ToString() :
26 null;