How to use GetMuxerArchitectureByPEHeaderOnWin method of Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.DotnetHostHelper class

Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.DotnetHostHelper.GetMuxerArchitectureByPEHeaderOnWin

DotnetHostHelper.cs

Source:DotnetHostHelper.cs Github

copy

Full Screen

...296 EqtTrace.Error($"DotnetHostHelper.GetMuxerFromGlobalRegistrationOnUnix: Exception during '{installLocation}' muxer resolution.\n{ex}");297 }298 return null;299 }300 private PlatformArchitecture? GetMuxerArchitectureByPEHeaderOnWin(string path)301 {302 try303 {304 using Stream stream = _fileHelper.GetStream(path, FileMode.Open, FileAccess.Read);305 using PEReader peReader = new(stream);306 switch (peReader.PEHeaders.CoffHeader.Machine)307 {308 case Machine.Amd64:309 return PlatformArchitecture.X64;310 case Machine.IA64:311 return PlatformArchitecture.X64;312 case Machine.Arm64:313 return PlatformArchitecture.ARM64;314 case Machine.Arm:315 return PlatformArchitecture.ARM;316 case Machine.I386:317 return PlatformArchitecture.X86;318 default:319 break;320 }321 }322 catch (Exception ex)323 {324 EqtTrace.Error($"DotnetHostHelper.GetMuxerArchitectureByPEHeaderOnWin: Failed to get architecture from PEHeader for '{path}'\n{ex}");325 }326 return null;327 }328 // See https://opensource.apple.com/source/xnu/xnu-2050.18.24/EXTERNAL_HEADERS/mach-o/loader.h329 // https://opensource.apple.com/source/xnu/xnu-4570.41.2/osfmk/mach/machine.h.auto.html330 private PlatformArchitecture? GetMuxerArchitectureByMachoOnMac(string path)331 {332 try333 {334 using var headerReader = _fileHelper.GetStream(path, FileMode.Open, FileAccess.Read);335 var magicBytes = new byte[4];336 var cpuInfoBytes = new byte[4];337 headerReader.Read(magicBytes, 0, magicBytes.Length);338 headerReader.Read(cpuInfoBytes, 0, cpuInfoBytes.Length);339 var magic = BitConverter.ToUInt32(magicBytes, 0);340 var cpuInfo = BitConverter.ToUInt32(cpuInfoBytes, 0);341 PlatformArchitecture? architecture = (MacOsCpuType)cpuInfo switch342 {343 MacOsCpuType.Arm64Magic or MacOsCpuType.Arm64Cigam => PlatformArchitecture.ARM64,344 MacOsCpuType.X64Magic or MacOsCpuType.X64Cigam => PlatformArchitecture.X64,345 MacOsCpuType.X86Magic or MacOsCpuType.X86Cigam => PlatformArchitecture.X86,346 _ => null,347 };348 return architecture;349 }350 catch (Exception ex)351 {352 // In case of failure during header reading we must fallback to the next place(default installation path)353 EqtTrace.Error($"DotnetHostHelper.GetMuxerArchitectureByMachoOnMac: Failed to get architecture from Mach-O for '{path}'\n{ex}");354 }355 return null;356 }357 internal enum MacOsCpuType : uint358 {359 Arm64Magic = 0x0100000c,360 Arm64Cigam = 0x0c000001,361 X64Magic = 0x01000007,362 X64Cigam = 0x07000001,363 X86Magic = 0x00000007,364 X86Cigam = 0x07000000365 }366 private bool IsValidArchitectureMuxer(PlatformArchitecture targetArchitecture, string path)367 {368 PlatformArchitecture? muxerPlatform = null;369 if (_environment.OperatingSystem == PlatformOperatingSystem.Windows)370 {371 muxerPlatform = GetMuxerArchitectureByPEHeaderOnWin(path);372 }373 else if (_environment.OperatingSystem == PlatformOperatingSystem.OSX)374 {375 muxerPlatform = GetMuxerArchitectureByMachoOnMac(path);376 }377 if (targetArchitecture != muxerPlatform)378 {379 EqtTrace.Verbose($"DotnetHostHelper.IsValidArchitectureMuxer: Incompatible architecture muxer, target architecture '{targetArchitecture}', actual '{muxerPlatform}'");380 return false;381 }382 EqtTrace.Verbose($"DotnetHostHelper.IsValidArchitectureMuxer: Compatible architecture muxer, target architecture '{targetArchitecture}', actual '{muxerPlatform}'");383 return true;384 }385}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful