Exemplo n.º 1
0
        public bool HasAssemblyAttribute(string name)
        {
            _assemblyImport.GetAssemblyFromScope(out uint assemblyScope);
            IMetaDataImport2 import2  = (IMetaDataImport2)_assemblyImport;
            IntPtr           valuePtr = IntPtr.Zero;

            import2.GetCustomAttributeByName(assemblyScope, name, out valuePtr, out uint valueLen);
            return(valueLen != 0);
        }
Exemplo n.º 2
0
        public Assembly(string assemblyName, string frameworkDir, string assemblyDir)
        {
            // resolved assembly path
            string assemblyPath;
            string name = assemblyName;
            //intermediate objects
            object rawMetaDataImportScope    = null;
            object rawMetaDataAsmImportScope = null;

            // save framework and assembly directories for further use
            this.frameworkDir = frameworkDir;
            this.assemblyDir  = assemblyDir;

            // First step: try to find the actual file

            if (System.IO.Path.GetExtension(name) != ".dll")
            {
                name += ".dll";
            }

            if (System.IO.File.Exists(System.IO.Path.Combine(assemblyDir, name)))
            {
                assemblyPath = System.IO.Path.Combine(assemblyDir, name);
            }
            else if (System.IO.File.Exists(System.IO.Path.Combine(frameworkDir, name)))
            {
                assemblyPath = System.IO.Path.Combine(frameworkDir, name);
            }
            else
            {
                assemblyPath = System.IO.Path.Combine(".", name);
            }
            // ??? At this point, we should try the GAC instead ???

            // open the assembly to retrieve the two COM interfaces to access data
            try
            {
                dispenser.OpenScope
                    (assemblyPath, 0x10, ref gMetaDataImportGuid, out rawMetaDataImportScope);
                dispenser.OpenScope
                    (assemblyPath, 0x10, ref gMetaDataAsmImportGuid, out rawMetaDataAsmImportScope);
            }
            catch
            {
                throw new System.ArgumentException("Cannot open " + assemblyPath + " or not a valid assembly.");
            }

            // let's convert the intermediate objects into the expected COM interfaces
            this.importIntf    = (IMetaDataImport2)rawMetaDataImportScope;
            this.asmImportIntf = (IMetaDataAssemblyImport)rawMetaDataAsmImportScope;

            // now that we have the COM interfaces OK, we can retrieve the assembly
            // information
            this.InitInformation();
        }
        public bool HasAssemblyAttribute(string name)
        {
            uint num;

            this._assemblyImport.GetAssemblyFromScope(out num);
            IMetaDataImport2 import  = (IMetaDataImport2)this._assemblyImport;
            IntPtr           zero    = IntPtr.Zero;
            uint             pcbData = 0;

            import.GetCustomAttributeByName(num, name.ToCharArray(), out zero, out pcbData);
            return(pcbData != 0);
        }
Exemplo n.º 4
0
        // Token: 0x06000E18 RID: 3608 RVA: 0x00037CB0 File Offset: 0x00035EB0
        public bool HasAssemblyAttribute(string name)
        {
            uint mdTokenObj;

            this._assemblyImport.GetAssemblyFromScope(out mdTokenObj);
            IMetaDataImport2 metaDataImport = (IMetaDataImport2)this._assemblyImport;
            IntPtr           zero           = IntPtr.Zero;
            uint             num            = 0u;

            metaDataImport.GetCustomAttributeByName(mdTokenObj, name, out zero, out num);
            return(num > 0u);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get the framework name from the assembly.
        /// </summary>
        private FrameworkName GetFrameworkName()
        {
            FrameworkName frameworkAttribute = null;

            try
            {
                IMetaDataImport2 import2  = (IMetaDataImport2)_assemblyImport;
                IntPtr           data     = IntPtr.Zero;
                UInt32           valueLen = 0;
                string           frameworkNameAttribute = null;
                UInt32           assemblyScope;

                _assemblyImport.GetAssemblyFromScope(out assemblyScope);
                int hr = import2.GetCustomAttributeByName(assemblyScope, s_targetFrameworkAttribute, out data, out valueLen);

                // get the AssemblyTitle
                if (hr == NativeMethodsShared.S_OK)
                {
                    // if an AssemblyTitle exists, parse the contents of the blob
                    if (NativeMethods.TryReadMetadataString(_sourceFile, data, valueLen, out frameworkNameAttribute))
                    {
                        if (!String.IsNullOrEmpty(frameworkNameAttribute))
                        {
                            frameworkAttribute = new FrameworkName(frameworkNameAttribute);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (ExceptionHandling.IsCriticalException(e))
                {
                    throw;
                }
            }

            return(frameworkAttribute);
        }
Exemplo n.º 6
0
		public static unsafe bool GetGenericParamNumAndAttrs(IMetaDataImport2 mdi2, uint token, out ushort number, out GenericParamAttributes attrs) {
			number = ushort.MaxValue;
			attrs = 0;
			if (mdi2 == null)
				return false;

			uint ulParamSeq, dwParamFlags;
			int hr = mdi2.GetGenericParamProps(token, new IntPtr(&ulParamSeq), new IntPtr(&dwParamFlags), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero);
			if (hr != 0)
				return false;

			number = (ushort)ulParamSeq;
			attrs = (GenericParamAttributes)dwParamFlags;
			return true;
		}
Exemplo n.º 7
0
		public static unsafe string GetGenericParamName(IMetaDataImport2 mdi2, uint token) {
			if (mdi2 == null)
				return null;

			char[] nameBuf = null;
			uint chName;
			int hr = mdi2.GetGenericParamProps(token, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0, new IntPtr(&chName));
			if (hr >= 0 && chName != 0) {
				nameBuf = new char[chName];
				fixed (char* p = &nameBuf[0])
					hr = mdi2.GetGenericParamProps(token, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, new IntPtr(p), (uint)nameBuf.Length, new IntPtr(&chName));
			}
			if (hr < 0)
				return null;

			if (chName <= 1)
				return string.Empty;
			return new string(nameBuf, 0, (int)chName - 1);
		}
Exemplo n.º 8
0
		public unsafe static uint GetGenericParamOwner(IMetaDataImport2 mdi2, uint token) {
			if (mdi2 == null)
				return 0;
			uint ownerToken;
			int hr = mdi2.GetGenericParamProps(token, IntPtr.Zero, IntPtr.Zero, new IntPtr(&ownerToken), IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero);
			if (hr != 0)
				return 0;
			var ownerMdToken = new MDToken(ownerToken);
			return ownerMdToken.Table == Table.TypeDef || ownerMdToken.Table == Table.Method ? ownerMdToken.Raw : 0;
		}
Exemplo n.º 9
0
		public unsafe static string GetModuleVersionString(IMetaDataImport2 mdi2) {
			if (mdi2 == null)
				return null;
			char[] nameBuf = null;
			uint ccBufSize = 0;
			int hr = mdi2.GetVersionString(IntPtr.Zero, 0, out ccBufSize);
			if (hr >= 0 && ccBufSize != 0) {
				nameBuf = new char[ccBufSize];
				fixed (char* p = &nameBuf[0])
					hr = mdi2.GetVersionString(new IntPtr(p), ccBufSize, out ccBufSize);
			}
			if (hr < 0)
				return null;

			if (ccBufSize <= 1)
				return string.Empty;
			return new string(nameBuf, 0, (int)ccBufSize - 1);
		}
Exemplo n.º 10
0
		public static Machine? GetModuleMachineAndPEKind(IMetaDataImport2 mdi2, out CorPEKind peKind) {
			peKind = 0;
			if (mdi2 == null)
				return null;

			uint dwPEKind, dwMachine;
			int hr = mdi2.GetPEKind(out dwPEKind, out dwMachine);
			if (hr != 0)
				return null;
			peKind = (CorPEKind)dwPEKind;
			return (Machine)dwMachine;
		}
Exemplo n.º 11
0
		public unsafe static uint[] GetGenericParamConstraintTokens(IMetaDataImport2 mdi2, uint token) {
			if (mdi2 == null)
				return new uint[0];

			IntPtr iter = IntPtr.Zero;
			try {
				uint cGenericParamConstraints;
				int hr = mdi2.EnumGenericParamConstraints(ref iter, token, IntPtr.Zero, 0, out cGenericParamConstraints);
				if (hr < 0)
					return new uint[0];

				uint ulCount = 0;
				hr = mdi2.CountEnum(iter, ref ulCount);
				if (hr < 0 || ulCount == 0)
					return new uint[0];

				hr = mdi2.ResetEnum(iter, 0);
				if (hr < 0)
					return new uint[0];

				uint[] gpcTokens = new uint[ulCount];
				fixed (uint* p = &gpcTokens[0])
					hr = mdi2.EnumGenericParamConstraints(ref iter, token, new IntPtr(p), (uint)gpcTokens.Length, out cGenericParamConstraints);
				if (hr < 0)
					return new uint[0];
				return gpcTokens;
			}
			finally {
				if (iter != IntPtr.Zero)
					mdi2.CloseEnum(iter);
			}
		}
Exemplo n.º 12
0
 public static void ReleaseContext()
 {
     importIntf = stack.Pop();
 }
Exemplo n.º 13
0
 public static void SetContext(IMetaDataImport2 intf)
 {
     stack.Push(importIntf);
     importIntf = intf;
 }
Exemplo n.º 14
0
		public unsafe static uint GetGenericParamConstraintOwnerRid(IMetaDataImport2 mdi2, uint token) {
			if (mdi2 == null)
				return 0;
			uint ownerToken;
			int hr = mdi2.GetGenericParamConstraintProps(token, new IntPtr(&ownerToken), IntPtr.Zero);
			if (hr != 0)
				return 0;
			var ownerMdToken = new MDToken(ownerToken);
			return ownerMdToken.Table == Table.GenericParam ? ownerMdToken.Rid : 0;
		}
Exemplo n.º 15
0
 public static void Init(Assembly asm)
 {
     mainAsm    = asm;
     importIntf = asm.importIntf;
 }
Exemplo n.º 16
0
		public unsafe static uint GetGenericParamConstraintTypeToken(IMetaDataImport2 mdi2, uint token) {
			if (mdi2 == null)
				return 0;
			uint tkConstraintType;
			int hr = mdi2.GetGenericParamConstraintProps(token, IntPtr.Zero, new IntPtr(&tkConstraintType));
			if (hr != 0)
				return 0;
			return tkConstraintType;
		}
Exemplo n.º 17
0
		public unsafe static byte[] GetMethodSpecProps(IMetaDataImport2 mdi2, uint token, out uint method) {
			method = 0;
			if (mdi2 == null)
				return null;

			IntPtr pvSigBlob;
			uint cbSigBlob;
			int hr = mdi2.GetMethodSpecProps(token, out method, out pvSigBlob, out cbSigBlob);
			if (hr != 0 || pvSigBlob == IntPtr.Zero)
				return null;

			byte[] sig = new byte[cbSigBlob];
			Marshal.Copy(pvSigBlob, sig, 0, sig.Length);
			return sig;
		}
Exemplo n.º 18
0
        /// <summary>
        /// Get the framework name from the assembly.
        /// </summary>
        private FrameworkName GetFrameworkName()
        {
// Disabling use of System.Reflection in case of MONO, because
// Assembly.GetCustomAttributes* for an attribute which belongs
// to an assembly that mono cannot find, causes a crash!
// Instead, opt for using PEReader and friends to get that info
#if FEATURE_ASSEMBLY_LOADFROM && !MONO
            if (!NativeMethodsShared.IsWindows)
            {
                if (String.Equals(Environment.GetEnvironmentVariable("MONO29679"), "1", StringComparison.OrdinalIgnoreCase))
                {
                    // Getting custom attributes in CoreFx contract assemblies is busted
                    // https://bugzilla.xamarin.com/show_bug.cgi?id=29679
                    return(null);
                }

                CustomAttributeData attr = null;

                foreach (CustomAttributeData a in _assembly.GetCustomAttributesData())
                {
                    try
                    {
                        if (a.AttributeType.Equals(typeof(TargetFrameworkAttribute)))
                        {
                            attr = a;
                            break;
                        }
                    }
                    catch
                    {
                    }
                }

                string name = null;
                if (attr != null)
                {
                    name = (string)attr.ConstructorArguments[0].Value;
                }
                return(name == null ? null : new FrameworkName(name));
            }

            FrameworkName frameworkAttribute = null;
            try
            {
                IMetaDataImport2 import2  = (IMetaDataImport2)_assemblyImport;
                IntPtr           data     = IntPtr.Zero;
                UInt32           valueLen = 0;
                string           frameworkNameAttribute = null;
                UInt32           assemblyScope;

                _assemblyImport.GetAssemblyFromScope(out assemblyScope);
                int hr = import2.GetCustomAttributeByName(assemblyScope, s_targetFrameworkAttribute, out data, out valueLen);

                // get the AssemblyTitle
                if (hr == NativeMethodsShared.S_OK)
                {
                    // if an AssemblyTitle exists, parse the contents of the blob
                    if (NativeMethods.TryReadMetadataString(_sourceFile, data, valueLen, out frameworkNameAttribute))
                    {
                        if (!String.IsNullOrEmpty(frameworkNameAttribute))
                        {
                            frameworkAttribute = new FrameworkName(frameworkNameAttribute);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (ExceptionHandling.IsCriticalException(e))
                {
                    throw;
                }
            }

            return(frameworkAttribute);
#else
            CorePopulateMetadata();
            return(_frameworkName);
#endif
        }
Exemplo n.º 19
0
        /// <summary>
        /// Get the framework name from the assembly.
        /// </summary>
        private FrameworkName GetFrameworkName()
        {
// Disabling use of System.Reflection in case of MONO, because
// Assembly.GetCustomAttributes* for an attribute which belongs
// to an assembly that mono cannot find, causes a crash!
// Instead, opt for using PEReader and friends to get that info
#if FEATURE_ASSEMBLY_LOADFROM && !MONO
            if (!NativeMethodsShared.IsWindows)
            {
                if (String.Equals(Environment.GetEnvironmentVariable("MONO29679"), "1", StringComparison.OrdinalIgnoreCase))
                {
                    // Getting custom attributes in CoreFx contract assemblies is busted
                    // https://bugzilla.xamarin.com/show_bug.cgi?id=29679
                    return(null);
                }

                CustomAttributeData attr = null;

                foreach (CustomAttributeData a in _assembly.GetCustomAttributesData())
                {
                    try
                    {
                        if (a.AttributeType.Equals(typeof(TargetFrameworkAttribute)))
                        {
                            attr = a;
                            break;
                        }
                    }
                    catch
                    {
                    }
                }

                string name = null;
                if (attr != null)
                {
                    name = (string)attr.ConstructorArguments[0].Value;
                }
                return(name == null ? null : new FrameworkName(name));
            }

            FrameworkName frameworkAttribute = null;
            try
            {
                IMetaDataImport2 import2  = (IMetaDataImport2)_assemblyImport;
                IntPtr           data     = IntPtr.Zero;
                UInt32           valueLen = 0;
                string           frameworkNameAttribute = null;
                UInt32           assemblyScope;

                _assemblyImport.GetAssemblyFromScope(out assemblyScope);
                int hr = import2.GetCustomAttributeByName(assemblyScope, s_targetFrameworkAttribute, out data, out valueLen);

                // get the AssemblyTitle
                if (hr == NativeMethodsShared.S_OK)
                {
                    // if an AssemblyTitle exists, parse the contents of the blob
                    if (NativeMethods.TryReadMetadataString(_sourceFile, data, valueLen, out frameworkNameAttribute))
                    {
                        if (!String.IsNullOrEmpty(frameworkNameAttribute))
                        {
                            frameworkAttribute = new FrameworkName(frameworkNameAttribute);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (ExceptionHandling.IsCriticalException(e))
                {
                    throw;
                }
            }

            return(frameworkAttribute);
#else
            using (var stream = File.OpenRead(_sourceFile))
                using (var peFile = new PEReader(stream))
                {
                    var metadataReader = peFile.GetMetadataReader();

                    var attrs = metadataReader.GetAssemblyDefinition().GetCustomAttributes()
                                .Select(ah => metadataReader.GetCustomAttribute(ah));

                    foreach (var attr in attrs)
                    {
                        var ctorHandle = attr.Constructor;
                        if (ctorHandle.Kind != HandleKind.MemberReference)
                        {
                            continue;
                        }

                        var container = metadataReader.GetMemberReference((MemberReferenceHandle)ctorHandle).Parent;
                        var name      = metadataReader.GetTypeReference((TypeReferenceHandle)container).Name;
                        if (!string.Equals(metadataReader.GetString(name), "TargetFrameworkAttribute"))
                        {
                            continue;
                        }

                        var arguments = GetFixedStringArguments(metadataReader, attr);
                        if (arguments.Count == 1)
                        {
                            return(new FrameworkName(arguments[0]));
                        }
                    }
                }
            return(null);
#endif
        }