我在使用 C# 时遇到问题,我想在我的代码中获取一个方法的指针,但这似乎是不可能的。我需要方法的指针,因为我想使用 WriteProcessMemory 不对其进行操作。我将如何获得指针?
示例代码
main()
{
function1();
function2();
}
function1()
{
//get function2 pointer
//use WPM to nop it (I know how, this is not the problem)
}
function2()
{
Writeline("bla"); //this will never happen because I added a no-op.
}
最佳答案
我知道这已经很老了,但是 C# 中的函数指针之类的示例如下所示:
class Temp
{
public void DoSomething() {}
public void DoSomethingElse() {}
public void DoSomethingWithAString(string myString) {}
public bool GetANewCat(string name) { return true; }
}
...然后在您的主要或任何地方:
var temp = new Temp();
Action myPointer = null, myPointer2 = null;
myPointer = temp.DoSomething;
myPointer2 = temp.DoSomethingElse;
然后调用原来的函数,
myPointer();
myPointer2();
如果您的方法有参数,那么它就像向您的 Action 添加通用参数一样简单:
Action<string> doItWithAString = null;
doItWithAString = temp.DoSomethingWithAString;
doItWithAString("help me");
或者如果你需要返回一个值:
Func<string, bool> getACat = null;
getACat = temp.GetANewCat;
var gotIt = getACat("help me");
关于C#函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9754669/