c#通过内存地址调用方法 [英] c# Call a method by memory address

查看:326
本文介绍了c#通过内存地址调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C#中的指定内存地址处调用函数,这是我在C中的处理方式:

I am trying to call a function at a specified memory address in c#, here is how I would go about it in C:

typedef void _do(int i);
auto doActor = (_do*)0xAAAABEEF;
doActor(1);

如果可能的话,如何在C#中复制此行为?对于C#来说是非常新的,感谢所有帮助.

How do I replicate this behavior in C# if at all possible? Very new to C# all help is appreciated.

更深入地说明我的情况:我正在尝试创建一个工具,该工具可以帮助我自动执行可执行文件中的某些进程.我反编译了主要API的程序集,并通过拦截网络数据包/欺骗自己的功能来添加了我的功能(大部分情况下).我无法控制客户端的事物,这些事物存储在无法反编译的模糊程序集中.我很少需要从这个混淆的程序集中调用一个或两个函数,并且我使用调试器找到了这些相关函数的内存地址,但是不知道在c#中从哪里可以找到.

More in depth explanation of my situation: I am trying to create a tool that helps me automate certain processes within an executable. I decompiled the assembly of the main API and added my functionality (for the most part) by intercepting network packets/spoofing my own. I do not have control over the client side of things, stored in an obfuscated assembly that I can't de-/recompile. I seldomly need to call one or two functions out of this obfuscated assembly and I found the memory addresses of those relevant functions using a debugger but don't know where to go from here in c#.

推荐答案

与C#中的C函数指针最接近的等效项是一个委托.代表是托管的,但是您可以使用Marshal.GetDelegateForFunctionPointer方法将它们彼此转换:

The closest equivalent to a C function pointer in C# is a delegate. Delegates are managed, but you can convert one from the other with the Marshal.GetDelegateForFunctionPointer method:

public delegate void _do(int i); // the delegate type

var ptr = new IntPtr(0xAAAABEEF);
var doActor = Marshal.GetDelegateForFunctionPointer<_do>(ptr);
doActor(1);

但是,与此API有一些限制.我建议您看看在文档中.

There are limitations tied to this API, however. I suggest you take a look at the documentation.

如果您使用的.NET版本早于4.5,则必须使用其他版本的GetDelegateForFunctionPointer:

If you're stuck with a version of .NET older than 4.5, you're going to have to use a different version of GetDelegateForFunctionPointer:

var doActor = (_do)Marshal.GetDelegateForFunctionPointer(ptr, typeof(_do));

这篇关于c#通过内存地址调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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