Exemplo n.º 1
0
        public static unsafe string GetMethodDefName(IMetaDataImport mdi, uint token, out MethodAttributes dwAttr, out MethodImplAttributes dwImplFlags)
        {
            dwAttr      = 0;
            dwImplFlags = 0;
            if (mdi == null)
            {
                return(null);
            }
            char[] nameBuf = null;
            uint   chMethod, cbSigBlob, ulCodeRVA;
            IntPtr pvSigBlob;
            int    hr = mdi.GetMethodProps(token, IntPtr.Zero, IntPtr.Zero, 0, out chMethod, out dwAttr, out pvSigBlob, out cbSigBlob, out ulCodeRVA, out dwImplFlags);

            if (hr >= 0 && chMethod != 0)
            {
                nameBuf = new char[chMethod];

                fixed(char *p = &nameBuf[0])
                hr = mdi.GetMethodProps(token, IntPtr.Zero, new IntPtr(p), (uint)nameBuf.Length, out chMethod, out dwAttr, out pvSigBlob, out cbSigBlob, out ulCodeRVA, out dwImplFlags);
            }
            if (hr < 0)
            {
                return(null);
            }

            if (chMethod <= 1)
            {
                return(string.Empty);
            }
            return(new string(nameBuf, 0, (int)chMethod - 1));
        }
Exemplo n.º 2
0
        public unsafe MethodProps GetMethodProps(uint methodToken)
        {
            MethodProps methodProps = new MethodProps();

            try
            {
                IntPtr sigPtr = IntPtr.Zero;
                methodProps.Token = methodToken;
                methodProps.Name  =
                    Util.GetString(delegate(uint pStringLenght, out uint stringLenght, System.IntPtr pString)
                {
                    uint sigBlobSize;
                    metaData.GetMethodProps(methodProps.Token,
                                            out methodProps.ClassToken,
                                            pString, pStringLenght, out stringLenght, // The string to get
                                            out methodProps.Flags,
                                            out sigPtr,                               //new IntPtr(&pSigBlob),
                                            out sigBlobSize,
                                            out methodProps.CodeRVA,
                                            out methodProps.ImplFlags);
                });
            }
            catch
            {
            }
            return(methodProps);
        }
Exemplo n.º 3
0
        public unsafe void GetMethodProps(uint mb, out string szMethod, out uint pClass, out uint pdwAttr, out byte[] ppvSigBlob, out uint pulCodeRVA,
                                          out uint pdwImplFlags)
        {
            uint  pchName, maxcbName = 50, lenSigBlob;
            byte *ptrBlob;

            byte[] buffer = new byte[2 * (maxcbName + 1)];

            importer.GetMethodProps(mb, out pClass, buffer, maxcbName, out pchName, out pdwAttr, out ptrBlob, out lenSigBlob, out pulCodeRVA, out pdwImplFlags);
            if (pchName > maxcbName)
            {
                buffer = new byte[2 * (pchName + 1)];
                importer.GetMethodProps(mb, out pClass, buffer, pchName, out pchName, out pdwAttr, out ptrBlob, out lenSigBlob, out pulCodeRVA, out pdwImplFlags);
            }
            szMethod   = UnicodeEncoding.Unicode.GetString(buffer).Substring(0, (int)pchName - 1);
            ppvSigBlob = pointerToArray(ptrBlob, lenSigBlob);
        }
Exemplo n.º 4
0
        private void getProps()
        {
            if (_import == null || _token == 0)
            {
                return;
            }

            _methodProps = new MethodProps();
            _import.GetMethodProps(_token, out _methodProps.classToken, _methodProps.szMethod, Convert.ToUInt32(_methodProps.szMethod.Length), out _methodProps.pchMethod, out _methodProps.pwdAttr, out _methodProps.ppvSigBlob, out _methodProps.pcbSigBlob, out _methodProps.pulCodeRva, out _methodProps.pdwImplFlags);
        }
Exemplo n.º 5
0
        public static MethodSig GetMethodSignature(IMetaDataImport mdi, uint token)
        {
            if (mdi == null)
            {
                return(null);
            }
            MethodAttributes     attrs;
            MethodImplAttributes implAttrs;
            uint   chMethod, cbSigBlob, ulCodeRVA;
            IntPtr pvSigBlob;
            int    hr = mdi.GetMethodProps(token, IntPtr.Zero, IntPtr.Zero, 0, out chMethod, out attrs, out pvSigBlob, out cbSigBlob, out ulCodeRVA, out implAttrs);

            if (hr < 0)
            {
                return(null);
            }

            byte[] sig = new byte[cbSigBlob];
            Marshal.Copy(pvSigBlob, sig, 0, sig.Length);
            return(new DebugSignatureReader().ReadSignature(mdi, sig) as MethodSig);
        }
Exemplo n.º 6
0
        public MethodProps GetMethodProps(uint methodToken)
        {
            MethodProps ret     = new MethodProps();
            IntPtr      sigPtr  = IntPtr.Zero;
            uint        sigSize = 0;

            ret.Token = methodToken;
            ret.Name  = Util.GetString(delegate(uint pStringLenght, out uint stringLenght, System.IntPtr pString) {
                metaData.GetMethodProps(
                    ret.Token,
                    out ret.ClassToken,
                    pString, pStringLenght, out stringLenght,
                    out ret.Flags,
                    out sigPtr,
                    out sigSize,
                    out ret.CodeRVA,
                    out ret.ImplFlags
                    );
            });
            ret.SigBlob = new Blob(sigPtr, sigSize);
            return(ret);
        }
Exemplo n.º 7
0
            static void _PrintArgs(ICorDebugILFrame ilframe, IMetaDataImport importer, System.IO.TextWriter writer)
            {
                uint argcount;
                {
                    ICorDebugValueEnum ven;
                    ilframe.EnumerateArguments(out ven);
                    ven.GetCount(out argcount);
                }
                if (argcount < 1)
                {
                    return;
                }

                uint ftoken;
                ilframe.GetFunctionToken(out ftoken);

                System.Reflection.MethodAttributes fattribs;
                {
                    uint chMethod;
                    uint dwAttr;
                    IntPtr pvSigBlob;
                    uint cbSigBlob;
                    uint ulCodeRVA;
                    uint dwImplFlags;
                    uint ftypedeftoken;
                    int hrmethodprops = (int)importer.GetMethodProps(ftoken, out ftypedeftoken,
                        null, 0, out chMethod,
                        out dwAttr,
                        out pvSigBlob, out cbSigBlob,
                        out ulCodeRVA, out dwImplFlags);
                    Marshal.ThrowExceptionForHR(hrmethodprops);
                    char[] methodnamechars = new char[chMethod];
                    hrmethodprops = (int)importer.GetMethodProps(ftoken, out ftypedeftoken,
                        methodnamechars, (uint)methodnamechars.Length, out chMethod,
                        out dwAttr,
                        out pvSigBlob, out cbSigBlob,
                        out ulCodeRVA, out dwImplFlags);
                    Marshal.ThrowExceptionForHR(hrmethodprops);
                    chMethod--; // Remove nul.
                    //MethodName = new string(methodnamechars, 0, (int)chMethod);
                    fattribs = (System.Reflection.MethodAttributes)dwAttr;
                }

                List<uint> paramtoks = new List<uint>();
                {
                    uint henum = 0;
                    try
                    {
                        uint[] aoneparamtok = new uint[1];
                        for (; ; )
                        {
                            uint count;
                            importer.EnumParams(ref henum, ftoken, aoneparamtok, 1, out count);
                            if (1 != count)
                            {
                                break;
                            }
                            paramtoks.Add(aoneparamtok[0]);
                        }
                    }
                    finally
                    {
                        importer.CloseEnum(henum);
                    }
                }

                uint ia = 0;
                int ip = 0; //paramtoks.Count - (int)argcount;
                for (; ia < argcount; ia++)
                {
                    string argname;
                    if (0 == ia
                            && System.Reflection.MethodAttributes.Static != (fattribs & System.Reflection.MethodAttributes.Static))
                    {
                        argname = "this";
                    }
                    else
                    {
                        if (ip >= paramtoks.Count)
                        {
                            argname = "unnamed_param_" + (1 + ia).ToString();
                        }
                        else
                        {
                            {
                                uint ptok = paramtoks[ip++];
                                uint parenttok;
                                uint pulSequence;
                                uint argnamelen = 0;
                                uint dwAttr, dwCPlusTypeFlag, cchValue;
                                IntPtr pValue;
                                importer.GetParamProps(ptok, out parenttok, out pulSequence, null, argnamelen, out argnamelen,
                                    out dwAttr, out dwCPlusTypeFlag, out pValue, out cchValue);
                                char[] argnamebuf = new char[argnamelen];
                                importer.GetParamProps(ptok, out parenttok, out pulSequence, argnamebuf, argnamelen, out argnamelen,
                                    out dwAttr, out dwCPlusTypeFlag, out pValue, out cchValue);
                                argnamelen--; // Remove nul.
                                argname = new string(argnamebuf, 0, (int)argnamelen);
                            }
                        }
                    }
                    string argstr;
                    {
                        ICorDebugValue pvalue;
                        ilframe.GetArgument(ia, out pvalue);
                        argstr = ToString(pvalue);
                    }
                    writer.WriteLine("{0}={1}", argname, argstr);
                }
            }
Exemplo n.º 8
0
		unsafe static CorPropertyInfo ReadPropertyInfo(IMetaDataImport mdi, uint token, CorType type) {
			if (mdi == null)
				return null;

			uint chProperty, dwPropFlags, mdSetter, mdGetter;
			int hr = mdi.GetPropertyProps(token, IntPtr.Zero, IntPtr.Zero, 0, out chProperty, out dwPropFlags, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out mdSetter, out mdGetter, IntPtr.Zero, 0, IntPtr.Zero);
			char[] nameBuf = null;
			if (hr >= 0 && chProperty != 0) {
				nameBuf = new char[chProperty];
				fixed (char* p = &nameBuf[0])
					hr = mdi.GetPropertyProps(token, IntPtr.Zero, new IntPtr(p), (uint)nameBuf.Length, out chProperty, out dwPropFlags, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out mdSetter, out mdGetter, IntPtr.Zero, 0, IntPtr.Zero);
			}
			if (hr < 0)
				return null;

			string name = chProperty <= 1 ? string.Empty : new string(nameBuf, 0, (int)chProperty - 1);

			var getSig = GetMethodSignature(mdi, mdGetter);
			var setSig = GetMethodSignature(mdi, mdSetter);

			if (getSig == null)
				return null;
			if (getSig.ParamsAfterSentinel != null)
				return null;
			if (getSig.GenParamCount != 0)
				return null;
			if (getSig.Params.Count != 0)
				return null;
			if (getSig.RetType.RemovePinnedAndModifiers().GetElementType() == ElementType.Void)
				return null;

			if (setSig != null && setSig.ParamsAfterSentinel != null)
				setSig = null;
			if (setSig != null && setSig.GenParamCount != 0)
				setSig = null;
			if (setSig != null && setSig.Params.Count != 1)
				setSig = null;
			if (setSig != null && setSig.RetType.RemovePinnedAndModifiers().GetElementType() != ElementType.Void)
				setSig = null;

			if (setSig != null && getSig.HasThis != setSig.HasThis)
				setSig = null;
			if (setSig != null && !Equals(getSig.RetType.RemovePinnedAndModifiers(), setSig.Params[0].RemovePinnedAndModifiers()))
				setSig = null;

			if (setSig == null)
				mdSetter = 0;

			MethodAttributes getMethodAttrs;
			MethodImplAttributes dwImplAttrs;
			IntPtr pvSigBlob;
			hr = mdi.GetMethodProps(mdGetter, IntPtr.Zero, IntPtr.Zero, 0, out chProperty, out getMethodAttrs, out pvSigBlob, out chProperty, out chProperty, out dwImplAttrs);
			if (hr < 0)
				return null;

			var browseState = GetDebuggerBrowsableState(mdi, token);

			return new CorPropertyInfo(type, token, mdGetter, mdSetter, name, getSig, setSig, getMethodAttrs, browseState);
		}
Exemplo n.º 9
0
		public static MethodSig GetMethodSignature(IMetaDataImport mdi, uint token) {
			if (mdi == null)
				return null;
			MethodAttributes attrs;
			MethodImplAttributes implAttrs;
			uint chMethod, cbSigBlob, ulCodeRVA;
			IntPtr pvSigBlob;
			int hr = mdi.GetMethodProps(token, IntPtr.Zero, IntPtr.Zero, 0, out chMethod, out attrs, out pvSigBlob, out cbSigBlob, out ulCodeRVA, out implAttrs);
			if (hr < 0)
				return null;

			byte[] sig = new byte[cbSigBlob];
			Marshal.Copy(pvSigBlob, sig, 0, sig.Length);
			return new DebugSignatureReader().ReadSignature(mdi, sig) as MethodSig;
		}
        public static int MethodGetAttributes(IMetaDataImport mdi, uint tk)
        {
            int dwAttributes;

            mdi.GetMethodProps(tk, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero, new IntPtr(&dwAttributes), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
            return dwAttributes;
        }
Exemplo n.º 11
0
		public static unsafe string GetMethodName(IMetaDataImport mdi, uint token) {
			if (mdi == null)
				return null;
			char[] nameBuf = null;
			uint chMethod;
			int hr = mdi.GetMethodProps(token, IntPtr.Zero, IntPtr.Zero, 0, new IntPtr(&chMethod), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
			if (hr >= 0 && chMethod != 0) {
				nameBuf = new char[chMethod];
				fixed (char* p = &nameBuf[0])
					hr = mdi.GetMethodProps(token, IntPtr.Zero, new IntPtr(p), (uint)nameBuf.Length, new IntPtr(&chMethod), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
			}
			if (hr < 0)
				return null;

			if (chMethod <= 1)
				return string.Empty;
			return new string(nameBuf, 0, (int)chMethod - 1);
		}
Exemplo n.º 12
0
        unsafe static CorPropertyInfo ReadPropertyInfo(IMetaDataImport mdi, uint token, CorType type)
        {
            if (mdi == null)
            {
                return(null);
            }

            uint chProperty, dwPropFlags, mdSetter, mdGetter;
            int  hr = mdi.GetPropertyProps(token, IntPtr.Zero, IntPtr.Zero, 0, out chProperty, out dwPropFlags, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out mdSetter, out mdGetter, IntPtr.Zero, 0, IntPtr.Zero);

            char[] nameBuf = null;
            if (hr >= 0 && chProperty != 0)
            {
                nameBuf = new char[chProperty];

                fixed(char *p = &nameBuf[0])
                hr = mdi.GetPropertyProps(token, IntPtr.Zero, new IntPtr(p), (uint)nameBuf.Length, out chProperty, out dwPropFlags, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out mdSetter, out mdGetter, IntPtr.Zero, 0, IntPtr.Zero);
            }
            if (hr < 0)
            {
                return(null);
            }

            string name = chProperty <= 1 ? string.Empty : new string(nameBuf, 0, (int)chProperty - 1);

            var getSig = GetMethodSignature(mdi, mdGetter);
            var setSig = GetMethodSignature(mdi, mdSetter);

            if (getSig == null)
            {
                return(null);
            }
            if (getSig.ParamsAfterSentinel != null)
            {
                return(null);
            }
            if (getSig.GenParamCount != 0)
            {
                return(null);
            }
            if (getSig.Params.Count != 0)
            {
                return(null);
            }
            if (getSig.RetType.RemovePinnedAndModifiers().GetElementType() == ElementType.Void)
            {
                return(null);
            }

            if (setSig != null && setSig.ParamsAfterSentinel != null)
            {
                setSig = null;
            }
            if (setSig != null && setSig.GenParamCount != 0)
            {
                setSig = null;
            }
            if (setSig != null && setSig.Params.Count != 1)
            {
                setSig = null;
            }
            if (setSig != null && setSig.RetType.RemovePinnedAndModifiers().GetElementType() != ElementType.Void)
            {
                setSig = null;
            }

            if (setSig != null && getSig.HasThis != setSig.HasThis)
            {
                setSig = null;
            }
            if (setSig != null && !Equals(getSig.RetType.RemovePinnedAndModifiers(), setSig.Params[0].RemovePinnedAndModifiers()))
            {
                setSig = null;
            }

            if (setSig == null)
            {
                mdSetter = 0;
            }

            MethodAttributes     getMethodAttrs;
            MethodImplAttributes dwImplAttrs;
            IntPtr pvSigBlob;

            hr = mdi.GetMethodProps(mdGetter, IntPtr.Zero, IntPtr.Zero, 0, out chProperty, out getMethodAttrs, out pvSigBlob, out chProperty, out chProperty, out dwImplAttrs);
            if (hr < 0)
            {
                return(null);
            }

            var browseState = GetDebuggerBrowsableState(mdi, token);

            return(new CorPropertyInfo(type, token, mdGetter, mdSetter, name, getSig, setSig, getMethodAttrs, browseState));
        }
        //need to parse methodsig
        public static uint MethodGetNumArg(IMetaDataImport mdi, uint tk)
        {
            uint cbSig;
            byte* pSig;
            mdi.GetMethodProps(tk, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero, (IntPtr)(&pSig), (IntPtr)(&cbSig), IntPtr.Zero, IntPtr.Zero);

            byte flags = *pSig++;
            uint cArgs = CorSigUncompressData(ref pSig);

            if ((flags & (byte)CorCallingConvention.IMAGE_CEE_CS_CALLCONV_HASTHIS) != 0)
                cArgs++;

            return cArgs;
        }
        public static string MethodGetName(IMetaDataImport mdi, uint tk)
        {
            uint chName;

            mdi.GetMethodProps(tk, IntPtr.Zero, IntPtr.Zero, 0, (IntPtr)(&chName), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            char* szName = stackalloc char[(int)chName];

            mdi.GetMethodProps(tk, IntPtr.Zero, (IntPtr)szName, chName, (IntPtr)(&chName), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
            return new string(szName);
        }
Exemplo n.º 15
0
		public unsafe static uint GetMethodOwnerRid(IMetaDataImport mdi, uint token) {
			if (mdi == null)
				return 0;
			if (IsGlobal(mdi, token))
				return 1;
			uint ownerToken;
			int hr = mdi.GetMethodProps(token, new IntPtr(&ownerToken), IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
			if (hr != 0)
				return 0;
			var ownerMdToken = new MDToken(ownerToken);
			return ownerMdToken.Table == Table.TypeDef ? ownerMdToken.Rid : 0;
		}
Exemplo n.º 16
0
		public unsafe static byte[] GetMethodSignatureBlob(IMetaDataImport mdi, uint token) {
			if (mdi == null)
				return null;
			uint cbSigBlob;
			IntPtr pvSigBlob;
			int hr = mdi.GetMethodProps(token, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero, new IntPtr(&pvSigBlob), new IntPtr(&cbSigBlob), IntPtr.Zero, IntPtr.Zero);
			if (hr < 0 || pvSigBlob == IntPtr.Zero)
				return null;

			var sig = new byte[cbSigBlob];
			Marshal.Copy(pvSigBlob, sig, 0, sig.Length);
			return sig;
		}
Exemplo n.º 17
0
		public static unsafe string GetMethodDefName(IMetaDataImport mdi, uint token, out MethodAttributes dwAttr, out MethodImplAttributes dwImplFlags) {
			dwAttr = 0;
			dwImplFlags = 0;
			if (mdi == null)
				return null;
			char[] nameBuf = null;
			uint chMethod, cbSigBlob, ulCodeRVA;
			IntPtr pvSigBlob;
			int hr = mdi.GetMethodProps(token, IntPtr.Zero, IntPtr.Zero, 0, out chMethod, out dwAttr, out pvSigBlob, out cbSigBlob, out ulCodeRVA, out dwImplFlags);
			if (hr >= 0 && chMethod != 0) {
				nameBuf = new char[chMethod];
				fixed (char* p = &nameBuf[0])
					hr = mdi.GetMethodProps(token, IntPtr.Zero, new IntPtr(p), (uint)nameBuf.Length, out chMethod, out dwAttr, out pvSigBlob, out cbSigBlob, out ulCodeRVA, out dwImplFlags);
			}
			if (hr < 0)
				return null;

			if (chMethod <= 1)
				return string.Empty;
			return new string(nameBuf, 0, (int)chMethod - 1);
		}
Exemplo n.º 18
0
		public static unsafe bool GetMethodAttributes(IMetaDataImport mdi, uint token, out MethodAttributes dwAttr, out MethodImplAttributes dwImplFlags) {
			dwAttr = 0;
			dwImplFlags = 0;
			if (mdi == null)
				return false;
			uint dwAttr2, dwImplFlags2;
			int hr = mdi.GetMethodProps(token, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero, new IntPtr(&dwAttr2), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, new IntPtr(&dwImplFlags2));
			if (hr < 0)
				return false;
			dwAttr = (MethodAttributes)dwAttr2;
			dwImplFlags = (MethodImplAttributes)dwImplFlags2;
			return true;
		}
        public static int MethodGetImplFlags(IMetaDataImport mdi, uint tk)
        {
            int dwImplFlags;

            mdi.GetMethodProps(tk, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, new IntPtr(&dwImplFlags));
            return dwImplFlags;
        }