首页
社区
课程
招聘
[求助][求助]如何用DynamicMethod实现带ref参数的方法
2010-9-17 12:15 5423

[求助][求助]如何用DynamicMethod实现带ref参数的方法

2010-9-17 12:15
5423
Dotnet保护过程中,除了名称流程混淆外,常用DynamicMethod保护部分方法,以增加读码的难度.
我在实现如下带ref参数的方法时,遇到了"方法标记不正确"的执行错误,所以请在这方面有过研究的高手指教.

下面是一个想用动态方法取代的目标例子:
        private bool refTest(string a, ref string b, ref string[] c)
        {
            b =  b+a;
            c[0] = c[0] + a;
            return true;
        }


而我的可编译通过,但运行时报错的例子如下:
private delegate bool refInvokeHand(string p0, ref string p1, ref string[] p2);

static refInvokeHand GetRefInvoke()
        {
            Type[] methodArgs = { typeof(string), typeof(string).MakeByRefType(), typeof(string[]).MakeByRefType() };
            DynamicMethod reftest2 = new DynamicMethod("reftest2", typeof(bool), methodArgs, typeof(Form1).Module);
            ILGenerator il = reftest2.GetILGenerator();
            il.Emit(OpCodes.Nop);
            ...
            ...
            ...

return (refInvokeHand)reftest2.CreateDelegate(typeof(refInvokeHand));
}
private bool refTest2(string a, ref string b, ref string[] c)
        {
              refInvokeHand myHand=GetRefInvoke();
              return myHand(a,ref b,ref c);   //[COLOR="Red"]此处运行时异常:方法标记不正确[/COLOR]
}




环境要求:.net2.0
其它:不考虑4.0及用类\结构代替多个ref参数的方法. 只求此种形式下的正确实现方式.

还请高手给予帮助!谢谢!!

[培训]二进制漏洞攻防(第3期);满10人开班;模糊测试与工具使用二次开发;网络协议漏洞挖掘;Linux内核漏洞挖掘与利用;AOSP漏洞挖掘与利用;代码审计。

收藏
点赞0
打赏
分享
最新回复 (3)
雪    币: 228
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
ssyfzy 2010-9-19 09:36
2
0
别人的动态方法好像直接以bytes为对象,而非构造IL

void DoDynamicMethod(byte[]methodBytes)
{
....
}
雪    币: 160
活跃值: (29)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
opensrc 1 2010-9-19 20:27
3
0
那是别人不想让人省事儿地看清楚,如果我写成这样:

static refInvokeHand<bool, string, string[]> GetRefInvoke()
        {
Type[] methodArgs = { typeof(string), typeof(string).MakeByRefType(), typeof(string[]).MakeByRefType() };
            DynamicMethod reftest2 = new DynamicMethod("reftest2", typeof(bool), methodArgs, typeof(Form1).Module);
            DynamicILInfo info = reftest2.GetDynamicILInfo();
            byte[] code = { 0x00, 0x03, 0x03, 0x50, 0x02,
                            0x28, 0x36, 0x00, 0x00, 0x0a, //6   
                            0x51, 0x04, 0x50, 0x16, 0x04,        
                            0x50, 0x16, 0x9a, 0x02,
                            0x28, 0x36, 0x00, 0x00, 0x0a, //21
                            0xa2, 0x17, 0x0a, 0x2b, 0x00,
                            0x06, 0x2a };
....
...
...
refInvokeHand<bool, string, string[]> invokerefTest2 = (refInvokeHand<bool, string, string[]>)reftest2.CreateDelegate(typeof(refInvokeHand<bool, string, string[]>));
           
            return invokerefTest2;
}

private bool refTest2(string a, ref string b, ref string[] c)
        {
              refInvokeHand myHand=GetRefInvoke();
              return myHand(a,ref b,ref c);   //此处运行时异常:方法标记不正确}

对于上述问题是同样的,但对于解答问题的网友来说,却要多费些周折.这个问题和方法体用何种形式表示没关系的.
谢谢你的热心,楼上的朋友.
雪    币: 160
活跃值: (29)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
opensrc 1 2010-9-19 20:35
4
0
[QUOTE=ssyfzy;860211]别人的动态方法好像直接以bytes为对象,而非构造IL

void DoDynamicMethod(byte[]methodBytes)
{
....
}[/QUOTE]

而且从你例举的例子来看,如果不是在某处有内定的静态变量可以被当参来利用的话,根本就是无参方法.
游客
登录 | 注册 方可回帖
返回