C# function pointer?

两盒软妹~` 提交于 2020-12-24 05:07:22

问题


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?

Example code

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.
}

回答1:


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;

Then to call the original function,

myPointer();
myPointer2();

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");



回答2:


EDIT: I misread your question and didn't see the bit about wanting to NOP a statement with doing raw memory manipulation. I'm afraid this isn't recommended because, as Raymond Chen says, the GC moves stuff around in memory (hence the 'pinned' keyword in C#). You probably can do it with reflection, but your question suggests you don't have a strong grasp of the CLR. Anyway, back to my original irrelevant answer (where I thought you just wanted information on how to use delegates):

C# isn't a scripting language ;)

Anyway, C# (and the CLR) has "function pointers" - except they're called "delegates" and are strongly typed, which means you need to define the function's signature in addition to the function you want to call.

In your case, you'd have something like this:

public static void Main(String[] args) {

    Function1();

}

// This is the "type" of the function pointer, known as a "delegate" in .NET.
// An instance of this delegate can point to any function that has the same signature (in this case, any function/method that returns void and accepts a single String argument).
public delegate void FooBarDelegate(String x); 


public static void Function1() {

    // Create a delegate to Function2
    FooBarDelegate functionPointer = new FooBarDelegate( Function2 );

    // call it
    functionPointer("bla");
}

public static void Function2(String x) {

    Console.WriteLine(x);
}



回答3:


public string myFunction(string name)
{
    return "Hello " + name;
}

public string functionPointerExample(Func<string,string> myFunction)
{
    return myFunction("Theron");
}

Func functionName.. use this to pass methods around. Makes no sense in this context but thats basically how you would use it




回答4:


I'd wish it is useful

class Program
{

    static void Main(string[] args)
    {
        TestPointer test = new TestPointer();
        test.function1();
    }
}
class TestPointer
{
    private delegate void fPointer(); // point to every functions that it has void as return value and with no input parameter
    public void function1()
    {
        fPointer point = new fPointer(function2);
        point();
    }
    private void function2()
    {
        Console.WriteLine("Bla");
    }
}



回答5:


Rewriting a method cannot be done directly from managed code, however the unmanaged .net profiling api can be used to do this. See this msdn article for example on how to use it.



来源:https://stackoverflow.com/questions/9754669/c-sharp-function-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!