private static string GetArchitectureForSource()

in src/Microsoft.TestPlatform.ObjectModel/Utilities/AssemblyLoadWorker.cs [268:370]


    private static string GetArchitectureForSource(string imagePath)
    {
        // For details refer to below code available on MSDN.
        // http://code.msdn.microsoft.com/CSCheckExeType-aab06100/sourcecode?fileId=22010&pathId=1874010322

        string archType = "AnyCPU";
        ushort machine = 0;

        uint peHeader;
        const int imageFileMachineAmd64 = 0x8664;
        const int imageFileMachineIa64 = 0x200;
        const int imageFileMachineI386 = 0x14c;
        const int imageFileMachineArm = 0x01c0;  // ARM Little-Endian
        const int imageFileMachineThumb = 0x01c2;  // ARM Thumb/Thumb-2 Little-Endian
        const int imageFileMachineArmnt = 0x01c4; // ARM Thumb-2 Little-Endian

        try
        {
            //get the input stream
            using Stream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
            bool validImage = true;

            BinaryReader reader = new(fs);
            //PE Header starts @ 0x3C (60). Its a 4 byte header.
            fs.Position = 0x3C;
            peHeader = reader.ReadUInt32();

            // Check if the offset is invalid
            if (peHeader > fs.Length - 5)
            {
                validImage = false;
            }
            if (validImage)
            {
                //Moving to PE Header start location...
                fs.Position = peHeader;

                UInt32 signature = reader.ReadUInt32(); //peHeaderSignature
                                                        // 0x00004550 is the letters "PE" followed by two terminating zeros.
                if (signature != 0x00004550)
                {
                    validImage = false;
                }

                if (validImage)
                {
                    //Read the image file header.
                    machine = reader.ReadUInt16();
                    reader.ReadUInt16(); //NumberOfSections
                    reader.ReadUInt32(); //TimeDateStamp
                    reader.ReadUInt32(); //PointerToSymbolTable
                    reader.ReadUInt32(); //NumberOfSymbols
                    reader.ReadUInt16(); //SizeOfOptionalHeader
                    reader.ReadUInt16(); //Characteristics

                    // magic number.32bit or 64bit assembly.
                    UInt16 magic = reader.ReadUInt16();
                    if (magic != 0x010B && magic != 0x020B)
                    {
                        validImage = false;
                    }
                }

                if (validImage)
                {
                    switch (machine)
                    {
                        case imageFileMachineI386:
                            archType = "X86";
                            break;

                        case imageFileMachineAmd64:
                        case imageFileMachineIa64:
                            archType = "X64";
                            break;

                        case imageFileMachineArm:
                        case imageFileMachineThumb:
                        case imageFileMachineArmnt:
                            archType = "ARM";
                            break;
                    }
                }
                else
                {
                    if (EqtTrace.IsVerboseEnabled)
                    {
                        EqtTrace.Verbose("Source path {0} is not a valid image path. Returning default proc arch type {1}.", imagePath, "AnyCPU");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //Ignore all exception
            if (EqtTrace.IsErrorEnabled)
            {
                EqtTrace.Error("AssemblyLoadWorker:GetArchitectureForSource() Returning default:{0}. Unhandled exception: {1}.", "AnyCPU", ex.ToString());
            }
        }

        return archType;
    }