C ++ / CLI显式地加载在运行时托管的DLL(调用LoadLibrary相当于对非托管) [英] C++/CLI Explicitly Load Managed DLL at runtime (the equivalent of LoadLibrary for Unmanaged)

查看:223
本文介绍了C ++ / CLI显式地加载在运行时托管的DLL(调用LoadLibrary相当于对非托管)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题1:

有没有办法在运行时,而不是在编译时在C ++ / CLI中显式地加载一个库。目前我使用的.NET添加引用在编译时。 我想明确地加载托管DLL。有没有在.NET相当于调用LoadLibrary的?

更新:由于Randolpho

大会:: LoadFrom从 MSDN

例子

 大会^ SampleAssembly;
SampleAssembly =汇编:: LoadFrom(C:\\ Sample.Assembly.dll);
//获取一个引用已知在装配中存在的方法。
MethodInfo的^方法= SampleAssembly-> GetTypes()[0]  - > GetMethod的(方法一);
//获取引用的MethodInfo实例的参数集合。
阵列<信息参数^> ^ PARAMS =&于方法GT; GetParameters();
//有关方法的参数显示的信息。
//参数= sParam1
//类型=系统::字符串
//位置= 0
//可选=假
每个(信息参数^参数在PARAMS)
{
   控制台:的WriteLine(参​​数= {0},参>名称);
   控制台:的WriteLine(类型= {0},参>参数类型);
   控制台:的WriteLine(位置= {0},参>的位置);
   控制台:的WriteLine(可选= {0},参> IsOptional);
}
 

问题2:

如果大会:: LoadFrom是.NET相当于调用LoadLibrary的。什么是GetProcAddress的等价?如何创建FunctionPointers的方法?

更新: MethodBase.Invoke从

 使用命名空间系统;
使用命名空间系统:反思;

公共引用类MagicClass
{
私人:
    INT magicBaseValue;

上市:
    MagicClass()
    {
        magicBaseValue = 9;
    }

    INT ItsMagic(INT preMagic)
    {
        返回preMagic * magicBaseValue;
    }
};

公共引用类TestMethodInfo
{
上市:
    静态无效的主要()
    {
        //获取构造,创造MagicClass的一个实例

        类型^ magicType =类型::的GetType(MagicClass);
        ConstructorInfo ^ magicConstructor = magicType-> GetConstructor(类型:: EmptyTypes);
        对象^ magicClassObject = magicConstructor->调用(gcnew阵列<对象^>(0));

        //获取ItsMagic方法和调用与100的参数值

        MethodInfo的^ magicMethod = magicType-> GetMethod的(ItsMagic);
        对象^ magicValue = magicMethod->调用(magicClassObject,gcnew阵列其中;对象^>(1){100});

        控制台:的WriteLine(MethodInfo.Invoke()示例\ N);
        控制台:的WriteLine(MagicClass.ItsMagic()返回:{0},magicValue);
    }
};

诠释的main()
{
    TestMethodInfo ::主();
}
 

解决方案

查看 http://www.informit.com/articles/article.aspx?p=25948 有关反射信息。可能是你正在寻找的门票(不知道更多的问题,就很难说了)

它有大约动态加载组件和探测他们找出方法和属性和诸如此类的东西一整节。

Problem 1:

Is there a way to explicitly load a library at runtime instead of at compile time in C++/CLI. Currently I am using the .NET "Add Reference" at compile time. I would like to explicitly load a managed dll. Is there the .NET equivalent of LoadLibrary?

Update: Thanks to Randolpho

Assembly::LoadFrom example from MSDN

Assembly^ SampleAssembly;
SampleAssembly = Assembly::LoadFrom( "c:\\Sample.Assembly.dll" );
// Obtain a reference to a method known to exist in assembly.
MethodInfo^ Method = SampleAssembly->GetTypes()[ 0 ]->GetMethod( "Method1" );
// Obtain a reference to the parameters collection of the MethodInfo instance.
array<ParameterInfo^>^ Params = Method->GetParameters();
// Display information about method parameters.
// Param = sParam1
//   Type = System::String
//   Position = 0
//   Optional=False
for each ( ParameterInfo^ Param in Params )
{
   Console::WriteLine( "Param= {0}", Param->Name );
   Console::WriteLine( "  Type= {0}", Param->ParameterType );
   Console::WriteLine( "  Position= {0}", Param->Position );
   Console::WriteLine( "  Optional= {0}", Param->IsOptional );
}

Problem 2:

If Assembly::LoadFrom is the .NET equivalent of LoadLibrary. What is the equivalent of GetProcAddress? How do I create FunctionPointers to the methods?

Update: MethodBase.Invoke from MSDN

using namespace System;
using namespace System::Reflection;

public ref class MagicClass
{
private:
    int magicBaseValue;

public:
    MagicClass()
    {
        magicBaseValue = 9;
    }

    int ItsMagic(int preMagic)
    {
        return preMagic * magicBaseValue;
    }
};

public ref class TestMethodInfo
{
public:
    static void Main()
    {
        // Get the constructor and create an instance of MagicClass

        Type^ magicType = Type::GetType("MagicClass");
        ConstructorInfo^ magicConstructor = magicType->GetConstructor(Type::EmptyTypes);
        Object^ magicClassObject = magicConstructor->Invoke(gcnew array<Object^>(0));

        // Get the ItsMagic method and invoke with a parameter value of 100

        MethodInfo^ magicMethod = magicType->GetMethod("ItsMagic");
        Object^ magicValue = magicMethod->Invoke(magicClassObject, gcnew array<Object^>(1){100});

        Console::WriteLine("MethodInfo.Invoke() Example\n");
        Console::WriteLine("MagicClass.ItsMagic() returned: {0}", magicValue);
    }
};

int main()
{
    TestMethodInfo::Main();
}

解决方案

Check out http://www.informit.com/articles/article.aspx?p=25948 for information about Reflection. Might be the ticket you're looking for (without knowing more of the issue, it's hard to say)

It has a whole section about dynamically loading assemblies and probing them to find out methods and properties and whatnot.

这篇关于C ++ / CLI显式地加载在运行时托管的DLL(调用LoadLibrary相当于对非托管)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆