Beispiel #1
0
        /// <summary>只反射加载指定路径的所有程序集</summary>
        /// <param name="file"></param>
        /// <param name="ver"></param>
        /// <returns></returns>
        public static Assembly ReflectionOnlyLoadFrom(String file, Version ver = null)
        {
            // 仅加载.Net文件,并且小于等于当前版本
            var pe = PEImage.Read(file);

            if (pe == null || !pe.IsNet)
            {
                return(null);
            }

            if (ver == null)
            {
                ver = new Version(Assembly.GetExecutingAssembly().ImageRuntimeVersion.TrimStart('v'));
            }

            // 只判断主次版本,只要这两个相同,后面可以兼容
            var pv = pe.Version;

            if (pv.Major > ver.Major || pv.Major == ver.Major && pv.Minor > ver.Minor)
            {
                if (XTrace.Debug)
                {
                    XTrace.WriteLine("程序集 {0} 的版本 {1} 大于当前运行时 {2}", file, pv, ver);
                }
                return(null);
            }
            // 必须加强过滤,下面一旦只读加载,就再也不能删除文件
            if (!pe.ExecutableKind.Has(PortableExecutableKinds.ILOnly))
            {
                // 判断x86/x64兼容。无法区分x86/x64的SQLite驱动
                //XTrace.WriteLine("{0,12} {1} {2}", item, pe.Machine, pe.ExecutableKind);
                //var x64 = pe.ExecutableKind.Has(PortableExecutableKinds.Required32Bit);
                //var x64 = pe.Machine == ImageFileMachine.AMD64;
                var x64 = pe.Machine == ImageFileMachine.AMD64;
                if (Runtime.Is64BitProcess ^ x64)
                {
                    if (XTrace.Debug)
                    {
                        XTrace.WriteLine("程序集 {0} 的代码特性是 {1},而当前进程是 {2} 进程", file, pe.Machine, Runtime.Is64BitProcess ? "64位" : "32位");
                    }
                    return(null);
                }
            }

            try
            {
                return(Assembly.ReflectionOnlyLoadFrom(file));
            }
            catch { return(null); }
            //return null;
        }
Beispiel #2
0
        /// <summary>只反射加载指定路径的所有程序集</summary>
        /// <param name="file"></param>
        /// <param name="ver"></param>
        /// <returns></returns>
        public static Assembly ReflectionOnlyLoadFrom(String file, Version ver = null)
        {
            // 仅加载.Net文件,并且小于等于当前版本
            if (!PEImage.CanLoad(file, ver, XTrace.Debug))
            {
                return(null);
            }

            try
            {
                return(Assembly.ReflectionOnlyLoadFrom(file));
            }
            catch { return(null); }
        }
Beispiel #3
0
        /// <summary>从数据流中读取PE文件头部</summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static PEImage Read(Stream stream)
        {
            if (stream.Length < 128)
            {
                return(null);
            }

            var reader = new BinaryReader(stream);

            // - DOSHeader DOS头部 固定64字节

            // PE					2
            // Start				58
            // Lfanew				4
            // End					64

            // 幻数
            if (reader.ReadUInt16() != 0x5a4d)
            {
                return(null);
            }

            stream.Seek(58, SeekOrigin.Current);

            // 最后4个字节是PEHeader位置
            stream.Position = reader.ReadUInt32();

            // 4字节PE文件头, PE\0\0
            if (reader.ReadUInt32() != 0x00004550)
            {
                return(null);
            }

            // - PEFileHeader

            var image = new PEImage
            {
                // 执行环境平台
                // Machine				2
                Machine = (ImageFileMachine)reader.ReadUInt16()
            };

            // NumberOfSections		2
            var sections = reader.ReadUInt16();

            // TimeDateStamp		4
            // PointerToSymbolTable	4
            // NumberOfSymbols		4
            // OptionalHeaderSize	2
            stream.Seek(14, SeekOrigin.Current);

            // 一个标志的集合,其大部分位用于OBJ或LIB文件中
            // Characteristics		2
            image.Characteristics = reader.ReadUInt16();

            image.ReadOptionalHeaders(reader);
            image.ReadSections(reader, sections);
            image.ReadCLIHeader(reader);
            image.ReadMetadata(reader);

            return(image);
        }
Beispiel #4
0
        /// <summary>从数据流中读取PE文件头部</summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static PEImage Read(Stream stream)
        {
            if (stream.Length < 128) return null;

            var reader = new BinaryReader(stream);

            // - DOSHeader DOS头部 固定64字节

            // PE					2
            // Start				58
            // Lfanew				4
            // End					64

            // 幻数
            if (reader.ReadUInt16() != 0x5a4d) return null;

            stream.Seek(58, SeekOrigin.Current);

            // 最后4个字节是PEHeader位置
            stream.Position = reader.ReadUInt32();

            // 4字节PE文件头, PE\0\0
            if (reader.ReadUInt32() != 0x00004550) return null;

            // - PEFileHeader

            var image = new PEImage();

            // 执行环境平台
            // Machine				2
            image.Machine = (ImageFileMachine)reader.ReadUInt16();

            // NumberOfSections		2
            var sections = reader.ReadUInt16();

            // TimeDateStamp		4
            // PointerToSymbolTable	4
            // NumberOfSymbols		4
            // OptionalHeaderSize	2
            stream.Seek(14, SeekOrigin.Current);

            // 一个标志的集合,其大部分位用于OBJ或LIB文件中
            // Characteristics		2
            image.Characteristics = reader.ReadUInt16();

            image.ReadOptionalHeaders(reader);
            image.ReadSections(reader, sections);
            image.ReadCLIHeader(reader);
            image.ReadMetadata(reader);

            return image;
        }