C ++ / CLI Explicitly loading a managed DLL at runtime (LoadLibrary equivalent for unmanaged)

Problem 1:

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

Update: Thanks 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 equivalent of a .NET LoadLibrary. What is equivalent to GetProcAddress? How to create FunctionPointers for 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(); } 
+4
source share
2 answers

See http://www.informit.com/articles/article.aspx?p=25948 for information on reflection. Perhaps this is the ticket you are looking for (without knowing more about the problem, it's hard to say)

It has a whole section about dynamically loading assemblies and examining them to find out methods and properties and much more.

0
source

Have you downloaded the managed DLL? Then you want Assembly::Load

+1
source

Source: https://habr.com/ru/post/1313664/


All Articles