C ++ / CLI在运行时显式加载托管DLL(相当于LoadLibrary for Unmanaged) [英] C++/CLI Explicitly Load Managed DLL at runtime (the equivalent of LoadLibrary for Unmanaged)
问题描述
问题1:
有没有办法在运行时显式加载库,而不是在C ++ / CLI中的编译时。目前我在编译时使用.NET添加参考。
我想显式加载一个托管dll。是否有相当于LoadLibrary的.NET?
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?
更新:感谢Randolpho
Update: Thanks to Randolpho
Assembly :: LoadFrom示例来自 MSDN
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 );
}
问题2:
如果Assembly :: LoadFrom是相当于LoadLibrary的.NET。 GetProcAddress是什么等价物?如何为方法创建FunctionPointer?
If Assembly::LoadFrom is the .NET equivalent of LoadLibrary. What is the equivalent of GetProcAddress? How do I create FunctionPointers to the methods?
更新: MethodBase.Invoke from MSDN
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();
}
推荐答案
查看 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.
这篇关于C ++ / CLI在运行时显式加载托管DLL(相当于LoadLibrary for Unmanaged)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!