C++/CLI 在运行时显式加载托管 DLL(相当于非托管的 LoadLibrary)

发布于 2024-09-07 06:00:28 字数 2545 浏览 3 评论 0原文

问题 1:

有没有办法在 C++/CLI 中在运行时而不是在编译时显式加载库。目前我在编译时使用.NET“添加引用”。 我想显式加载托管 dll。 .NET 中是否有 LoadLibrary 的等效项?

更新: 感谢

MSDN 中的 Randolpho Assembly::LoadFrom 示例

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

问题 2:

如果 Assembly::LoadFrom 是 LoadLibrary 的 .NET 等效项。 GetProcAddress 的等价物是什么?如何创建指向方法的函数指针?

更新: 来自 MSDN 的 MethodBase.Invoke

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

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

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

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

发布评论

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

评论(2

〆凄凉。 2024-09-14 06:00:36

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

它有一整节关于动态加载程序集并探测它们以找出方法和属性之类的内容。

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.

初吻给了烟 2024-09-14 06:00:34

你说的是托管 DLL吗?然后你需要 Assembly::Load

Did you say managed DLL? Then you want Assembly::Load

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