>
> Hey JB,
>
Of course I can show you some code, here you go:
private static List<MethodReference> ReferenceList = new
List<MethodReference>();
private static void FindReferences(AssemblyDefinition AssemblyDef)
{
foreach (TypeDefinition TypeDef in
AssemblyDef.MainModule.GetTypes())
{
for (int i = 0; i < TypeDef.Methods.Count; i++)
{
MethodDefinition MethodDef = TypeDef.Methods[i];
if (!MethodDef.IsConstructor)
{
foreach (Instruction InstructionDef in
MethodDef.Body.Instructions)
{
if (InstructionDef.OpCode == OpCodes.Call)
{
if (InstructionDef.Operand is
MethodReference)
{
MethodReference MethodRef =
(MethodReference)InstructionDef.Operand;
if (!ReferenceList.Contains(MethodRef))
{
Console.ForegroundColor =
ConsoleColor.Red;
Console.WriteLine("Name : " +
MethodDef.Name);
Console.ForegroundColor =
ConsoleColor.Gray;
CreateMethod(TypeDef,
MethodRef.Parameters.ToArray(), MethodRef.ReturnType, MethodRef);
ReferenceList.Add(MethodRef);
}
}
}
}
}
}
}
}
private static Random rnd = new Random();
private static string RandomName()
{
StringBuilder StrBuilder = new StringBuilder();
char[] charset = "abcdefg123".ToCharArray();
for (int i = 0; i < charset.Length; i++)
{
int rndindex = rnd.Next(charset.Length);
StrBuilder.Append(charset[rndindex]);
}
return StrBuilder.ToString();
}
private static void CreateMethod(TypeDefinition TypeDef,
ParameterDefinition[] Params, TypeReference MethodDefRetType,
MethodReference MethodRef)
{
Console.WriteLine("DeclaringType : " +
MethodRef.DeclaringType.ToString());
Console.WriteLine("MethodName : " + MethodRef.Name);
if (MethodDefRetType != null)
{
MethodDefinition NewMethod = new
MethodDefinition(RandomName(), MethodAttributes.Static, MethodDefRetType);
if (Params.Length > 0)
{
for (int i = 1; i < MethodRef.Parameters.Count; i++)
{
ParameterDefinition pd = MethodRef.Parameters[i];
NewMethod.Parameters.Add(pd);
NewMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg, pd)); //
Here is the main problem...
}
}
NewMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Call,
TypeDef.Module.Import(MethodRef)));
NewMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
TypeDef.Methods.Add(NewMethod);
Console.WriteLine("Added a new method...");
}
}
private static void PrintParams(MethodReference MethodRef)
{
foreach (ParameterDefinition ParameterDef in
MethodRef.Parameters)
{
Console.WriteLine("Param : " +
ParameterDef.ParameterType.ToString());
}
}
When I used it on a function which has no parameters, it works fine:
<http://puu.sh/cEjGT/32ca919dbf.png>
if the function has one or more parameters (like the msgbox method) :
<http://puu.sh/cEjJ8/53d44ca86d.png>
gives me this:
<http://puu.sh/cEjLz/9f0825e87d.png>
The thing is, if I know there is one parameter and I use ldarg.0 instead,
it works, but the second one cant be ldarg.0 aswell, i would have to use
ldarg.1.
But what if the method has like a bunch of parameters?
Later I would have to use ldarg without any suffix.
How can I load the parameters before the call independend from the amount
of parameters.
--
--
--
mono-cecil
---
You received this message because you are subscribed to the Google Groups
"mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.