O
O
Ogoun Er2011-11-02 17:04:32
.NET
Ogoun Er, 2011-11-02 17:04:32

Code injection in .NET?

The task is this:
There is an assembly in which third-party assemblies are loaded (ClickOnce applications are loaded from a remote server).
Next, I want to find all the windows of the loaded assembly (this is done), find their Activate method (too), and inject my code to intercept the creation of the window.
Moreover, it is required to make the injected code refer to the method of the main assembly.
I found how to do it through Mono.Cecil, but I want to implement it only using Framework'a.
Here is an example code:

private static void InjectWindow(Assembly app)
    {
      Type[] types = app.GetTypes();
      foreach (Type type in types)
      {
        //Если базовый тип выбранного типа - Окно, инъекцируем
        if (type.BaseType.Name.Equals("Window", StringComparison.OrdinalIgnoreCase))
        {
            MethodInfo activate = type.GetMethod("Activate");
            //Теперь надо как-то дописать свой код
        }
      }
    }

In mono at this stage, you can already call
activate.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Ldstr, "Inject!"));
How to do without it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question