The first issue I can see without running your code is that you can't
use a ParameterDefinition from a method in another.

So you can't write:

ParameterDefinition pd = MethodRef.Parameters[i];
NewMethod.Parameters.Add(pd);

You need to write:

ParameterDefinition parameter = new
ParameterDefinition(MethodRef.Parameters[i].ParameterType);
NewMethod.Parameters.Add(parameter)
NewMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg, parameter));

Jb

On Wed, Nov 5, 2014 at 4:44 PM, BlackSky Blacksky
<[email protected]> wrote:
>> 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:
>
> if the function has one or more parameters (like the msgbox method) :
>
> gives me this:
>
>
> 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.

-- 
-- 
--
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.

Reply via email to