Skip to content

Commit 1b97c95

Browse files
committed
remove dependency on native GetHostname
1 parent b5cfc57 commit 1b97c95

3 files changed

Lines changed: 2 additions & 55 deletions

File tree

src/System.Management.Automation/CoreCLR/CorePsExtensions.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,32 +1070,14 @@ public static string UserName
10701070
}
10711071
}
10721072

1073-
internal static string WinGetMachineName()
1074-
{
1075-
// In future release of operating systems, you might be able to rename a machine without
1076-
// rebooting. Therefore, don't cache this machine name.
1077-
StringBuilder buf = new StringBuilder(MaxMachineNameLength);
1078-
int len = MaxMachineNameLength;
1079-
if (Win32Native.GetComputerName(buf, ref len) == 0)
1080-
throw new InvalidOperationException(CoreClrStubResources.CannotGetComputerName);
1081-
return buf.ToString();
1082-
}
1083-
10841073
/// <summary>
10851074
/// MachineName
10861075
/// </summary>
10871076
public static string MachineName
10881077
{
10891078
get
10901079
{
1091-
if (Platform.IsWindows)
1092-
{
1093-
return WinGetMachineName();
1094-
}
1095-
else
1096-
{
1097-
return Platform.NonWindowsGetMachineName();
1098-
}
1080+
return System.Environment.MachineName;
10991081
}
11001082
}
11011083

@@ -1276,9 +1258,6 @@ internal static class Win32Native
12761258
// Win32 return type is BOOLEAN (which is 1 byte and not BOOL which is 4bytes)
12771259
internal static extern byte GetUserNameEx(int format, [Out] StringBuilder domainName, ref uint domainNameLen);
12781260

1279-
[DllImport("api-ms-win-downlevel-kernel32-l2-1-0.dll", CharSet = CharSet.Unicode, SetLastError = true)]
1280-
internal extern static int GetComputerName([Out]StringBuilder nameBuffer, ref int bufferSize);
1281-
12821261
[DllImport("api-ms-win-core-localization-l1-2-1.dll", CharSet = CharSet.Unicode)]
12831262
internal static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId,
12841263
int dwLanguageId, [Out]StringBuilder lpBuffer,

src/System.Management.Automation/CoreCLR/CorePsPlatform.cs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -351,18 +351,6 @@ internal static string NonWindowsGetUserName()
351351
}
352352
}
353353

354-
internal static string NonWindowsGetMachineName()
355-
{
356-
if (!IsWindows)
357-
{
358-
return LinuxPlatform.HostName;
359-
}
360-
else
361-
{
362-
throw new PlatformNotSupportedException();
363-
}
364-
}
365-
366354
// Hostname in this context seems to be the FQDN
367355
internal static string NonWindowsGetHostName()
368356
{
@@ -470,22 +458,6 @@ public static string[] CommandLineToArgv(string command)
470458

471459
internal static class LinuxPlatform
472460
{
473-
public static string HostName
474-
{
475-
get
476-
{
477-
StringBuilder value = new StringBuilder(1024);
478-
uint valueLen = (uint)value.Capacity;
479-
string computerName = Native.GetComputerName();
480-
if (string.IsNullOrEmpty(computerName))
481-
{
482-
int lastError = Marshal.GetLastWin32Error();
483-
throw new InvalidOperationException("LinuxPlatform.HostName error: " + lastError);
484-
}
485-
return computerName;
486-
}
487-
}
488-
489461
private static string _userName;
490462
public static string UserName
491463
{
@@ -696,10 +668,6 @@ internal static class Native
696668
[return: MarshalAs(UnmanagedType.LPStr)]
697669
internal static extern string GetUserName();
698670

699-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
700-
[return: MarshalAs(UnmanagedType.LPStr)]
701-
internal static extern string GetComputerName();
702-
703671
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
704672
internal static extern int GetLinkCount([MarshalAs(UnmanagedType.LPStr)]string filePath, out int linkCount);
705673

test/csharp/test_CorePsPlatform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static void TestGetMachineName()
9999
// The process should return an exit code of 0 on success
100100
Assert.Equal(0, process.ExitCode);
101101
// It should be the same as what our platform code returns
102-
Assert.Equal(hostname, Platform.NonWindowsGetMachineName());
102+
Assert.Equal(hostname, System.Management.Automation.Environment.MachineName);
103103
}
104104
}
105105

0 commit comments

Comments
 (0)