Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# function pointer?

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. } 
like image 321
user1276333 Avatar asked Mar 17 '12 23:03

user1276333


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language w3schools?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

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"); 
like image 64
outbred Avatar answered Sep 29 '22 21:09

outbred


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); } 
like image 37
Dai Avatar answered Sep 29 '22 21:09

Dai