C# 函数指针?

扫码查看
本文介绍了C# 函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 C# 时遇到了问题,我想在我的代码中获取一个方法的指针,但这似乎是不可能的.我需要该方法的指针,因为我想使用 WriteProcessMemory 不操作它.我将如何获得指针?

I'm having a problem with C#, I'd like to get a pointer of a method in my code, but it seems impossible. I need the pointer of the method because I want to no-op it using WriteProcessMemory. How would I get the pointer?

示例代码

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# 中,像函数指针这样的例子应该是这样的:

I know this is very old, but an example of something like a function pointer in C# would be like this:

class Temp
{
   public void DoSomething() {}
   public void DoSomethingElse() {}
   public void DoSomethingWithAString(string myString) {}
   public bool GetANewCat(string name) { return true; }
}

...然后在您的主要或任何地方:

...and then in your main or wherever:

var temp = new Temp();
Action myPointer = null, myPointer2 = null;
myPointer = temp.DoSomething;
myPointer2 = temp.DoSomethingElse;

然后调用原函数,

myPointer();
myPointer2();

如果你的方法有参数,那么就像给你的 Action 添加通用参数一样简单:

If you have arguments to your methods, then it's as simple as adding generic arguments to your Action:

Action<string> doItWithAString = null;
doItWithAString = temp.DoSomethingWithAString;

doItWithAString("help me");

或者如果你需要返回一个值:

Or if you need to return a value:

Func<string, bool> getACat = null;
getACat = temp.GetANewCat;

var gotIt = getACat("help me");

这篇关于C# 函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 10:27
查看更多