/// <summary> /// Find the Method for this method on the given type. /// </summary> internal Method findMethod(string qname, string methodName, string[] paramTypes, string returnType) { string key = getMethodKey(qname, methodName, paramTypes); Method method = (Method)methods[key]; if (method == null) { // this stuff should be the same for both cases PERWAPI.Type rtype = findType(returnType); PERWAPI.Type[] pars = new PERWAPI.Type[paramTypes.Length]; for (int i = 0; i < paramTypes.Length; i++) { pars[i] = findType(paramTypes[i]); } // check for stubs object obj = findType(qname); if (obj is ClassDef) { // class is an internal stub, so we need to stub this // method out too, which should get flushed out later ClassDef cdef = obj as ClassDef; // TODO - need to fix attrs Param[] mpars = new Param[pars.Length]; for (int i = 0; i < mpars.Length; i++) { mpars[i] = new Param(ParamAttr.Default, "v" + i, pars[i]); } // TODO - fix param names // Use Public here for stub - real MethAttr will be set // when method is flushed out method = cdef.AddMethod(MethAttr.Public, ImplAttr.IL, methodName, rtype, mpars); } else if (obj is ClassRef) { // class is external, just get or add ref ClassRef cref = obj as ClassRef; method = cref.GetMethod(methodName, pars, new PERWAPI.Type[0]); if (method == null) { method = cref.AddMethod(methodName, rtype, pars); } } else { throw new System.Exception("Don't know how to handle: " + obj.GetType()); } // make sure we add method to hashtable methods[key] = method; } return(method); }
//private static bool GEtMethodsFromClass(ClassDef methodClass, string strMethodName, out Method TagetMethod) //{ // TagetMethod = null; // Method tmpTagetMethod = methodClass.GetMethodDesc(strMethodName); // if (tmpTagetMethod != null) // { // TagetMethod = tmpTagetMethod; // return true; // } // return false; //} private static bool GetLoggerMethodsFromClass(ClassRef methodLogger, out Method startLogMethod, out Method endLogMethod) { startLogMethod = endLogMethod = null; MethodRef tempStartLogMethod = methodLogger.GetMethod(startLogMethodName); MethodRef tempEndLogMethod = methodLogger.GetMethod(endLogMethodName); if (tempStartLogMethod != null && tempEndLogMethod != null) { if (CheckLogMethodParameters(tempStartLogMethod.GetParTypes()) && CheckLogMethodParameters(tempEndLogMethod.GetParTypes())) { startLogMethod = tempStartLogMethod; endLogMethod = tempEndLogMethod; return(true); } } return(false); }
//internal static ClassRef GetClass(string strClassName) //{ // if (string.IsNullOrEmpty(strClassName)) // strClassName = "Myexce"; // string str1 = Process.GetCurrentProcess().MainModule.FileName; // //Console.WriteLine(str1); // PEFile file = PEFile.ReadPEFile(str1); // string Assemblyname = System.IO.Path.GetFileNameWithoutExtension(str1); ; // AssemblyRef newAssemblyRef = file.MakeExternAssembly(Assemblyname); // ClassRef newMethodLoggerRef = TryGetMethodLoggerFromAssembly(newAssemblyRef, "MethodExecLogger"); // return newMethodLoggerRef; //} internal static bool GEtMethodsFromClass(ClassRef methodClass, string strMethodName, out Method TagetMethod) { TagetMethod = null; MethodRef tmpTagetMethod = methodClass.GetMethod(strMethodName); if (tmpTagetMethod != null) { TagetMethod = tmpTagetMethod; return(true); } return(false); }
//public static bool LocateLoggerMethods(PEFile file, string assemblyName, string className, out Method startLogMethod, out Method endLogMethod) //{ // startLogMethod = endLogMethod = null; // // Check if it is in this assembly itself // if (file.GetThisAssembly().Name() == assemblyName) // { // ClassDef methodLogger = file.GetClass(className); // if (methodLogger != null) // { // return GetLoggerMethodsFromClass(methodLogger, out startLogMethod, out endLogMethod); // } // } // // Check referenced assemblies // foreach (AssemblyRef assemblyRef in file.GetImportedAssemblies()) // { // if (assemblyRef.Name() == assemblyName) // { // ClassRef methodLoggerRef = TryGetMethodLoggerFromAssembly(assemblyRef, className); // if (methodLoggerRef != null) // { // if (GetLoggerMethodsFromClass(methodLoggerRef, out startLogMethod, out endLogMethod)) // return true; // } // } // } // // Not found in this assembly or referenced assemblies. Try loading given assembly and adding it as reference // AssemblyRef newAssemblyRef = file.MakeExternAssembly(assemblyName); // ClassRef newMethodLoggerRef = TryGetMethodLoggerFromAssembly(newAssemblyRef, className); // if (newMethodLoggerRef != null) // { // if (GetLoggerMethodsFromClass(newMethodLoggerRef, out startLogMethod, out endLogMethod)) // return true; // } // return false; //} private static ClassRef TryGetMethodLoggerFromAssembly(PEFile pef, AssemblyRef assemblyRef, string className) { //string fileName = ""; //if (assemblyRef.Name().ToLower().EndsWith(".dll")|| assemblyRef.Name().ToLower().EndsWith(".exe")) // fileName = assemblyRef.Name(); //else // fileName = assemblyRef.Name() + ".exe"; ////Console.WriteLine("TryGetMethodLoggerFromAssembly ->" + fileName); //if (!System.IO.File.Exists(fileName)) //{ // Console.WriteLine(fileName + " not present in current directory. Skipping it in search"); // return null; //} PEFile refFile = pef;//PEFile.ReadPEFile(fileName); ClassDef methodLogger = refFile.GetClass(className); if (methodLogger != null) { ClassRef methodLoggerRef = methodLogger.MakeRefOf(); if (assemblyRef.GetClass(className) == null) { assemblyRef.AddClass(methodLoggerRef); } System.Array.ForEach(methodLogger.GetMethods(), delegate(MethodDef methodDef) { if (methodLoggerRef.GetMethod(methodDef.Name()) == null) { methodLoggerRef.AddMethod(methodDef.Name(), methodDef.GetRetType(), methodDef.GetParTypes()); } }); return(methodLoggerRef); } return(null); }