Gilad

I'm trying to add a prolog to some method. This prolog should call a method in some assembly. I added the reference to the new assembly and I'm able to call methods in it but only with somple arguments. See the following sig.

BYTE Sig_void_String[] = {

0, // IMAGE_CEE_CS_CALLCONV_DEFAULT

0x1, // argument count

0x1, // ret = ELEMENT_TYPE_VOID

ELEMENT_TYPE_I4

};

I would like to pass an object, the class pointer; i.e. this. I understand that the OpCode should be 0x02 0x28 but I don't know what to write instead of ELEMENT_TYPE_I4.

Please Help.



Re: Building Development and Diagnostic Tools for .Net How to add code in JitCompilationStarted to call method with an object as srgument

Sergey Kanzhelev

It's depends on your method's signature. Simpliest way is to make the method to accept System.Object. And in that method cast to known type (if type is really known). In this situation you have to change ELEMENT_TYPE_I4 to ELEMENT_TYPE_OBJECT.

public void MyMethod(Object thisObject)

{

((MyClass)thisObject).TestString = "AfterInjection";

}

public class MyClass

{

public MethodToInjectTo()

{

//injected code start:

ldarg.0

call token of { 0, 0x1, 0x1, ELEMENT_TYPE_OBJECT }

//injected code end

Some code

}

public string TestString = "BeforeInjection";

}

The other possibility is to create methop with signature, that accept concrete class. That make you to change ELEMENT_TYPE_I4 to ELEMENT_TYPE_CLASS and then compressed token of your class.

public void MyMethod(MyClass thisObject)

{

thisObject.TestString = "AfterInjection";

}

public class MyClass

{

public MethodToInjectTo()

{

//injected code start:

ldarg.0

call token of { 0, 0x1, 0x1, ELEMENT_TYPE_CLASS, CorSigCompressToken(MyClass) }

//injected code end

Some code

}

public string TestString = "BeforeInjection";

}





Re: Building Development and Diagnostic Tools for .Net How to add code in JitCompilationStarted to call method with an object as srgument

Gilad

Sergey, you are a genius !

I'm going to post another question regarding the profiler and will appreciate your help there.