remove dependency on native GetHostname

This commit is contained in:
George Fleming
2016-05-05 09:08:06 -07:00
parent b5cfc575d3
commit 1b97c954ea
3 changed files with 2 additions and 55 deletions
@@ -1070,17 +1070,6 @@ namespace System.Management.Automation
}
}
internal static string WinGetMachineName()
{
// In future release of operating systems, you might be able to rename a machine without
// rebooting. Therefore, don't cache this machine name.
StringBuilder buf = new StringBuilder(MaxMachineNameLength);
int len = MaxMachineNameLength;
if (Win32Native.GetComputerName(buf, ref len) == 0)
throw new InvalidOperationException(CoreClrStubResources.CannotGetComputerName);
return buf.ToString();
}
/// <summary>
/// MachineName
/// </summary>
@@ -1088,14 +1077,7 @@ namespace System.Management.Automation
{
get
{
if (Platform.IsWindows)
{
return WinGetMachineName();
}
else
{
return Platform.NonWindowsGetMachineName();
}
return System.Environment.MachineName;
}
}
@@ -1276,9 +1258,6 @@ namespace System.Management.Automation
// Win32 return type is BOOLEAN (which is 1 byte and not BOOL which is 4bytes)
internal static extern byte GetUserNameEx(int format, [Out] StringBuilder domainName, ref uint domainNameLen);
[DllImport("api-ms-win-downlevel-kernel32-l2-1-0.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal extern static int GetComputerName([Out]StringBuilder nameBuffer, ref int bufferSize);
[DllImport("api-ms-win-core-localization-l1-2-1.dll", CharSet = CharSet.Unicode)]
internal static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId,
int dwLanguageId, [Out]StringBuilder lpBuffer,
@@ -351,18 +351,6 @@ namespace System.Management.Automation
}
}
internal static string NonWindowsGetMachineName()
{
if (!IsWindows)
{
return LinuxPlatform.HostName;
}
else
{
throw new PlatformNotSupportedException();
}
}
// Hostname in this context seems to be the FQDN
internal static string NonWindowsGetHostName()
{
@@ -470,22 +458,6 @@ namespace System.Management.Automation
internal static class LinuxPlatform
{
public static string HostName
{
get
{
StringBuilder value = new StringBuilder(1024);
uint valueLen = (uint)value.Capacity;
string computerName = Native.GetComputerName();
if (string.IsNullOrEmpty(computerName))
{
int lastError = Marshal.GetLastWin32Error();
throw new InvalidOperationException("LinuxPlatform.HostName error: " + lastError);
}
return computerName;
}
}
private static string _userName;
public static string UserName
{
@@ -696,10 +668,6 @@ namespace System.Management.Automation
[return: MarshalAs(UnmanagedType.LPStr)]
internal static extern string GetUserName();
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
[return: MarshalAs(UnmanagedType.LPStr)]
internal static extern string GetComputerName();
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
internal static extern int GetLinkCount([MarshalAs(UnmanagedType.LPStr)]string filePath, out int linkCount);
+1 -1
View File
@@ -99,7 +99,7 @@ namespace PSTests
// The process should return an exit code of 0 on success
Assert.Equal(0, process.ExitCode);
// It should be the same as what our platform code returns
Assert.Equal(hostname, Platform.NonWindowsGetMachineName());
Assert.Equal(hostname, System.Management.Automation.Environment.MachineName);
}
}