late binding, InvokeMember managed C++
Hi I am trying to load a c# dll at runtime and call a method in it from managed C++, I am not having much luck
I am trying t use
Assembly::LoadFrom and
System::Type^ typ
typ->InvokeMember
methods.
anyone have sample code in managed C++.
Urgent. Please and thank you.
Indika
Re: late binding, InvokeMember managed C++
Re: late binding, InvokeMember managed C++
This works for me (using VC++ 2010 Express):
Code:
// TestDLL.h
#pragma once
using namespace System;
namespace TestDLL {
public ref class Class1
{
public:
static void SayHello();
};
}
Code:
// TestDLL.cpp
#include "stdafx.h"
#include "TestDLL.h"
using namespace TestDLL;
void Class1::SayHello()
{
Console::WriteLine("Hello from the DLL!");
}
Code:
// MCppDllTest.cpp: Hauptprojektdatei.
#include "stdafx.h"
using namespace System;
using namespace System::Reflection;
int main(array<System::String ^> ^args)
{
#ifdef _DEBUG
String ^strDllName = "..\\Debug\\TestDLL.dll";
#else
String ^strDllName = "..\\Release\\TestDLL.dll";
#endif
Console::WriteLine("Hello World from the main app");
Assembly ^ably = Assembly::LoadFrom(strDllName);
Type ^cls = ably->GetType("TestDLL.Class1");
cls->InvokeMember("SayHello", BindingFlags::Static | BindingFlags::Public | BindingFlags::InvokeMethod,
nullptr, nullptr, nullptr);
Console::WriteLine("Back in the main app");
Console::ReadLine();
return 0;
}
Does this model your scenario, more or less? Ok, the DLL is C++/CLI here because I don't have C# installed, but I suspect the invoking part to be the problematic one anyway. If your scenario differs significantly in any aspect (except the C#), please let me know. And a more detailed description of the problem than "I am not having much luck" would have been helpful, of course. ;)
It took me some time to put that together, including lots of debugging. But now this one works. :) I'm relatively sure, however, that I had it this way before and it did not work. :confused: But never mind, now...
Re: late binding, InvokeMember managed C++
Thanks... yes that will do the trick. I had already fixed with similar code as yours.