prettyCode
prettyCode

Reputation: 103

Can Reflection help me call a function in an injected DLL?

I have injected a managed .NET DLL into a .NET process.
I've seen some people here on StackOverflow say that you can then call the functions of the injected DLL by using Reflection. This is apparently the technique that Snoop uses.
Is this correct? If so, exactly how could it be done?
Thank you in advance.

Upvotes: 0

Views: 989

Answers (4)

WeNeedAnswers
WeNeedAnswers

Reputation: 4644

Great Article by Eric Gunnerson, only caveat is to watch out for security policies, as these can sometime prevent dynamic loading of assemblies.

http://blogs.msdn.com/b/ericgu/archive/2007/06/05/app-domains-and-dynamic-loading-the-lost-columns.aspx

Upvotes: 0

decyclone
decyclone

Reputation: 30830

Here is some sample code to do this:

        // Get all loaded assemblies in current application domain
        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

        // Get type of int
        Type intType = assemblies.Select(a => a.GetType("System.Int32")).First();

        // Create object of int using its type
        Object intObj = Activator.CreateInstance(intType);

        // Call int.ToString() method which returns '0'
        String result = intObj.GetType().GetMethod("ToString", new Type[] { }).Invoke(intObj, null).ToString();

Upvotes: 0

TalentTuner
TalentTuner

Reputation: 17556

see below

Reflection Examples

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

You could use reflection. Here's an example:

class Program
{
    static void Main()
    {
        var assembly = Assembly.Load("System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
        var serverType = assembly.GetType("System.Web.HttpUtility", true);
        var method = serverType.GetMethod("HtmlEncode", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(string) }, null);
        var result = method.Invoke(null, new[] { "<some value>" });
        Console.WriteLine(result);
    }
}

Upvotes: 2

Related Questions