3

Assume I have a process (target.exe) which is running on my computer. I found out, that target.exe is using a DLL (target.dll). In this DLL there is a method named:

public PaintedObject GetRepaintedObject(Image img)
{
   return new PaintedObject(img);
}

Is it possible to catch the method call GetRepaintedObject at runtime and execute my own code ? I just want to return my own PaintedObject.

Edit: I think target.exe loads the target.dll into his memory space and this is the point where I want to inject or modify the method GetRepaintedObject. I don't want recompile the dll or something else. Everytime target.exe is calling GetRepaintedObject I want to intercept this and return my own value.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ngine
  • 65
  • 8
  • Look in to the Mono.Cecil library. There are plenty of tutorials on it. – Scott Chamberlain Dec 13 '16 at 15:53
  • I already did that. But I didn't found any examples or information about my problem. – Ngine Dec 13 '16 at 16:00
  • For the duplicate you just need to replace step `5b` with a the code you want to run instead. If the code is more than a line or two long I would suggest making a `inject.dll` and have the `GetRepaintedObject` call a static function from `inject.dll` and return that static method's call. If you have trouble calling a static method from another dll in Mono.Cecil ask a new quesiton specifically about that. – Scott Chamberlain Dec 13 '16 at 18:02
  • This does not answer my question. This example does only write into his own thread or into a custom stream. I want to inject my code iinto the target.exe process. Target.exe has his own thread which loads the DLL into his memory space and this is where I want to inject my code. – Ngine Dec 14 '16 at 10:07
  • Yes it does, you modify `target.dll` before `target.exe` is started, when `target.exe` starts normally it loads your modified version of `target.dll` and runs the code you added to it. – Scott Chamberlain Dec 14 '16 at 13:55
  • "Is it possible to catch the method call GetRepaintedObject at **runtime** and execute my own code ? I just want to return my own PaintedObject"" ... This was my question. The keyword is **runtime**. I don't want to recompile the dll and replace it. And the given example you marked as an answer of my question explains how to modify the dll in custom thread and not how to modify the dll in the target.exe thread. Again: I want to modify the dll which is already loaded into the target.exe process. No restart of the process or anything. – Ngine Dec 14 '16 at 14:27
  • Ok, I have re-opened your question and added clarification to the title to diffrentate it from the previous duplicate. – Scott Chamberlain Dec 14 '16 at 14:38
  • You may want to look in to this SO question and answer for some ideas http://stackoverflow.com/questions/6169552/is-there-a-way-to-hook-a-managed-function-in-c-sharp-like-i-would-a-unmanaged-fu – Scott Chamberlain Dec 14 '16 at 14:52

1 Answers1

-1

Do you need to call GetRepaintedObject() from DLL at run-time?

If yes, you can get the reference to methods of classes defined in DLL using Reflection.

Gurdev Singh
  • 1,996
  • 13
  • 11
  • I don't need to call the method. The method is called by target.exe sometimes and I just want to intercept this call and run my own code. So every time **GetRepaintedObject** is called by **target.exe**. I want to execute/inject my own code and return a custom **PaintedObject* to **target.exe**: – Ngine Dec 13 '16 at 15:44