在C#中获取方法地址 [英] Obtaining method address in C#

查看:105
本文介绍了在C#中获取方法地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在vb6中,我曾经使用ObjPtr(functionname)来获取类内方法的地址,那里有一些(低级或无证件的确定,但尽可能直接的[fast]手段).原因是我想尝试修补vtable并将一些asm推入该方法中..
至于代表,我在本文中进行了测试:
http://www.codeproject.com/KB/audio-video/PWR.aspx
事实证明,委托给RtlMoveMemory -10 *的时间更长-移动字节数组然后是直接副本(.Net中过多的白痴证明).所以..代表们出去了.有没有文件证明?有没有一种方法可以获取课程地址,因此我可以尝试GetProcAddress?

谢谢,
John

解决方案

查看 私有 GCHandle _hndCompare; 私有 IntPtr _addCompare; 公共 Utils() { 对象 mcmp =(对象). Compare(" " "); GCHandle _hndCompare = GCHandle.Alloc(mcmp,GCHandleType.Pinned); IntPtr _addCompare = _hndCompare.AddrOfPinnedObject(); } 公共 int Compare( string str1,字符串 str2) { int ret = 0 ; 返回 ret; } 〜实用程序() { 尝试 { 如果(_hndCompare.IsAllocated) { _hndCompare.Free(); _addCompare = IntPtr .0; } } 捕获 {} }


In vb6 I used to use ObjPtr(functionname) to get the address of a method inside a class, is there some (low level or undocumented ok, but as direct [fast] a means as possible). The reason is I''d like to try and patch vtable and push some asm into the method..
As for Delegates, I did a test in this article:
http://www.codeproject.com/KB/audio-video/PWR.aspx
Turns out it takes delegate to RtlMoveMemory -10* longer- to move a byte array then a straight copy, (way too much idiot proofing in .Net). So.. delegates are out. Anything undocumented? Is there a way to get class address so I can try GetProcAddress?

thanks,
John

Look at Delegates[^] - they are effectively function pointers under a different name, but with parameter type safety built in.


This gets me a handle (to something), but is probably too slow:

private GCHandle _hndCompare;
private IntPtr _addCompare;

public Utils()
{
    Object mcmp = (Object)this.Compare("","");
    GCHandle _hndCompare = GCHandle.Alloc(mcmp, GCHandleType.Pinned);
    IntPtr _addCompare = _hndCompare.AddrOfPinnedObject();
}

public int Compare(string str1, string str2)
{
    int ret = 0;
    return ret;
}

~Utils()
{
    try
    {
        if (_hndCompare.IsAllocated)
        {
            _hndCompare.Free();
            _addCompare = IntPtr.Zero;
        }
    }
    catch { }
}


这篇关于在C#中获取方法地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆