问题一:
有没有办法在 C++/CLI 中在运行时而不是在编译时显式加载库。目前我在编译时使用.NET“添加引用”。
我想显式加载托管 dll。 .NET 中是否有 LoadLibrary 的等效项?
Update:感谢兰多夫
Assembly::LoadFrom 示例来自MSDN http://msdn.microsoft.com/en-us/library/1009fa28.aspx
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 的等价物是什么?如何创建指向方法的函数指针?
Update:MethodBase.调用自MSDN http://msdn.microsoft.com/en-us/library/a89hcwhh.aspx
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();
}
你刚才说managed动态链接库?那么你想要Assembly::Load http://msdn.microsoft.com/en-us/library/system.reflection.assembly.load.aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)