反射可以帮助我调用注入的 DLL 中的函数吗?

发布于 2024-10-07 12:48:56 字数 137 浏览 0 评论 0原文

我已将托管 .NET DLL 注入到 .NET 进程中。
我在 StackOverflow 上看到有人说你可以使用反射来调用注入的 DLL 的函数。这显然是 Snoop 使用的技术。
这是正确的吗?如果是的话,具体该怎么做?
先感谢您。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

徒留西风 2024-10-14 12:48:56

您可以使用反射。这是一个例子:

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

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);
    }
}
喜爱纠缠 2024-10-14 12:48:56

以下是执行此操作的一些示例代码:

        // 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();

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();
调妓 2024-10-14 12:48:56

Eric Gunnerson 的精彩文章,唯一需要注意的是要注意安全策略,因为这些策略有时会阻止程序集的动态加载。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文