Skip to content

Commit e627ad6

Browse files
author
Robert Holt
committed
Avoid System.Runtime.InteropServices.RuntimeInformation.OSArchitecture in win7 on .NET Framework
1 parent bb65b2a commit e627ad6

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/PowerShellEditorServices.Host/EditorServicesHost.cs

+28-3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ public void StartLogging(string logFilePath, LogLevel logLevel)
154154

155155
string osVersion = RuntimeInformation.OSDescription;
156156

157+
string osArch = GetOSArchitecture();
158+
157159
string buildTime = BuildInfo.BuildTime?.ToString("s", System.Globalization.CultureInfo.InvariantCulture) ?? "<unspecified>";
158160

159161
string logHeader = $@"
@@ -164,12 +166,12 @@ public void StartLogging(string logFilePath, LogLevel logLevel)
164166
Name: {this.hostDetails.Name}
165167
Version: {this.hostDetails.Version}
166168
ProfileId: {this.hostDetails.ProfileId}
167-
Arch: {RuntimeInformation.OSArchitecture}
169+
Arch: {osArch}
168170
169171
Operating system details:
170172
171173
Version: {osVersion}
172-
Arch: {RuntimeInformation.OSArchitecture}
174+
Arch: {osArch}
173175
174176
Build information:
175177
@@ -245,7 +247,7 @@ await this.editorSession.PowerShellContext.ImportCommandsModule(
245247
// gets initialized when that is done earlier than LanguageServer.Initialize
246248
foreach (string module in this.additionalModules)
247249
{
248-
var command =
250+
var command =
249251
new System.Management.Automation.PSCommand()
250252
.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
251253
.AddParameter("Name", module);
@@ -493,6 +495,29 @@ private IServerListener CreateServiceListener(MessageProtocolType protocol, Edit
493495
}
494496
}
495497

498+
/// <summary>
499+
/// Gets the OSArchitecture for logging. Cannot use System.Runtime.InteropServices.RuntimeInformation.OSArchitecture
500+
/// directly, since this tries to load API set DLLs in win7 and crashes.
501+
///
502+
/// <returns></returns>
503+
private string GetOSArchitecture()
504+
{
505+
#if !CoreCLR
506+
// If on win7 (version 6.1.x), avoid System.Runtime.InteropServices.RuntimeInformation
507+
if (Environment.OSVersion.Platform.Equals("Win32NT") && Environment.OSVersion.Version < new Version(6, 2))
508+
{
509+
if (Environment.Is64BitProcess)
510+
{
511+
return "X64";
512+
}
513+
514+
return "X86";
515+
}
516+
#endif
517+
518+
return RuntimeInformation.OSArchitecture.ToString();
519+
}
520+
496521
#endregion
497522
}
498523
}

0 commit comments

Comments
 (0)