开发者

C# Hook to another dlls method?

开发者 https://www.devze.com 2023-02-23 00:42 出处:网络
Is it possible to \"attach\" to another C# dll\'s method(don\'t have the source for that dll)? What I mean is that if there is another program on the machine using that dll and it makes a call to the

Is it possible to "attach" to another C# dll's method(don't have the source for that dll)? What I mean is that if there is another program on the machine using that dll and it makes a call to the said method i will get an event in my program.

I'm having problems with adding functionality to an existing application that is written in c# t开发者_高级运维o which I don't have the source. I need an additional form that will pop up when there is a call to the specific method in a program.

The program connects to an MS SQL database on a different machine. I was also thinking about creating an SQL trigger on the server that would somehow connect with the computer and send some sort of signal to trigger the additional form but that failed miserably.

[Edit] Alternatively could I listen on the machine for SQL traffic and if I find a call for an SQL procedure that accompany's that dll method my program would act? But won't that affect performance?


It isn't well documented, and I myself am still trying to figure out how it all really works, but here is a c# solution for hooking into DLLs: EasyHook


The term for the action you want to do is Dll Injection. Here is a tutorial on how to get your .net code running in a injected dll. If you want a introduction on doing DLL inejection the same person has a very good first timers tutorial called "How do I build a working poker bot." It is very thorough and very simple to understand.


It is plausible, if certain conditions were met in the "other" dll.

  1. If the "other" dll has a mechanism for you to get a reference to instance of the object instantiated in a separate App Domain.
  2. If the event you are interest in has an access of "public".

Then you can create a delegate and assign that delegate to your chosen method.

It would look something like:

var other = OtherDLL.getInstance(InstanceID);
other.DidSomething += new EventHandler(myDelegate);
// the += is important so that you are adding your delegate to the existing calls for that method..

//Where you have:
protected myDelegate(Object sender, EventArgs e)
{
// do what you want...
}

If the public hooks are not available, then you can't do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消