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.
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() :