all groups > dotnet clr > july 2005 >
You're in the

dotnet clr

group:

IMetaDataEmit DefineUserString



IMetaDataEmit DefineUserString Matt Garven
7/28/2005 12:00:00 AM
dotnet clr: Has anyone successfully used DefineUserString as part of IL modification?
I'm trying to change the IL of a method during JITCompilationStarted but as
soon as I introduce ldstr calls i'm running into InvalidProgramFormat
exceptions.

My understanding is that the mdString token created by DefineUserString
should be able to be used with ldstr so I can emit calls to methods that
take string parameters.

I've searched around but the examples I found didn't work either - same
exception... eg:
http://www.mail-archive.com/dotnet-rotor@discuss.develop.com/msg00510.html

I'm using the .NET Framework 1.1 SP 1, if that is relevant.

Thanks.

- Matt Garven

RE: IMetaDataEmit DefineUserString v-jetan NO[at]SPAM online.microsoft.com (
7/29/2005 8:44:13 AM
Hi Matt,

Thanks for your post.

Yes, I think this should can be done. Because I found some internal record
regarding this issue: using DefineUserString to do IL modification for
certain application. Although I have not tried this yet.

For the link you tried, it is a rotor sample code. Do you used rotor to try
it or do you modifiy it and try it on .Net Framework?(Based on my
experience, to use it in .Net Framework, we have to modify some of this
source code) If you try this source code on rotor, can you tell me which OS
you did the test? Normally, Rotor can only run successfully before
WindowsXP SP2, on my Win2003 machine, it will fail...

If you do the code modification and test it in .Net Frameowrk 1.1, can you
provide me your test sample project, then we can do some test and reproduce
out your problem. Thanks.

Finally, if you want to get more information about IL modification, there
is a wonderful MSDN article talk about this:
"Rewrite MSIL Code on the Fly with the .NET Framework Profiling API "
http://msdn.microsoft.com/msdnmag/issues/03/09/NETProfilingAPI/

Hope this helps
==========================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Re: IMetaDataEmit DefineUserString Matt Garven
8/3/2005 12:00:00 AM
Hi Jeffrey,

Thanks for your reply.

I was not using rotor, I was using the .Net Framework. The rotor example
from the page I included in my first post was pretty similar to what I was
trying to achieve. If I could get that example working for the .Net
Framework I could easily adapt it to suit my purposes.

Thanks heaps! :)

Regards,
Matt Garven

[quoted text, click to view]

Re: IMetaDataEmit DefineUserString Matt Garven
8/3/2005 12:00:00 AM
Hi Jeffrey,

Thanks yes I can get it working when not using strings, integers like in the
example you have provided are not a problem.

The problem I'm having at the moment relates to calling code using string
parameters, more specifically those that are created using DefineUserString.

Regards,
Matt Garven

[quoted text, click to view]

Re: IMetaDataEmit DefineUserString v-jetan NO[at]SPAM online.microsoft.com (
8/3/2005 9:10:34 AM
Hi Matt,

Thanks for your feedback.

Yes, I have used Matt Pietrek's DNProfiler sample in MSDN Magazine, 2001 to
test the code. I also will get InvalidProgramException with the code, so it
seems that there is some problem in the code.

I still did not have time to find the root cause problem to the code.
Anyway, I have tried the IL rewriting code snippet in the article below,
and it works well on my side:
"IL modification at runtime (step III)"
http://blog.monstuff.com/archives/000077.html

Note: to success use the code, we should first write an Exe applicaiton
with the code:
using System;

public class Hello
{
public static void Main(string[] prms)
{
Console.WriteLine("main!");
blah(4,5);
}

public static void blah(int i, int j) {
Console.WriteLine("blah!");
Console.WriteLine(i);
Console.WriteLine(j);
}
}

Then, we should place an assembly with the name logger.dll containing code
below:
using System;

namespace DumkyNamespace
{
public class Logger
{
public static void Log(int i)
{
Console.WriteLine("Log!" + i);
}
}
}

Hope this helps
===============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Re: IMetaDataEmit DefineUserString v-jetan NO[at]SPAM online.microsoft.com (
8/4/2005 8:40:56 AM
Hi Matt,

Thanks for your feedback.

Ok, I see your concern. You want to get a sample to use DefineUserString
with IL rewriting.

Currently, I have written an email to "Jan Kotas"(he is the SDE of our CLR
team) for his sample problem. I will reply to you as soon as there is any
update. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Re: IMetaDataEmit DefineUserString v-jetan NO[at]SPAM online.microsoft.com (
8/5/2005 7:56:33 AM
Hi Matt,

Sorry for letting you wait.

I have contacted "Jan Kotas" for this article. And he has confirmed that
there is a little bug in the sample code snippet. We should change
ILCode.Flags_CodeSize = 0x42; to ILCode.Flags_CodeSize = 0x2E; . I have
applied this change and everything works well now.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Re: IMetaDataEmit DefineUserString Matt Garven
8/8/2005 12:00:00 AM
Thanks for the help Jeffrey.

I'll give it a shot and let you know how I go.

Regards,
Matt Garven

[quoted text, click to view]

Re: IMetaDataEmit DefineUserString v-jetan NO[at]SPAM online.microsoft.com (
8/8/2005 9:33:58 AM
Ok, I will wait for your further feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
AddThis Social Bookmark Button