Hey!
I have a managed c++ app (actually, the app is mixed managed/unmanaged, but
this happens in the managed part) that dynamically generates some program in
c# (with CSharpCodeProvi der).
The problem now is that somehow I get runtime errors when trying to pass a
function (via delegates) into the c# code in order to make c# call back that
function. The message box says: "An unhandled exeption of type
'System.Argumen tException' occurred in mscorlib.dll Additional information:
The object with type my_delegate cannot be converted to type my_delegate.'
(Actually, this is a translation of my message.)
I have given both delegate declarations the same name in case this was the
error.
Maybe someone can help?
Best regards + many thanks,
Andreas
A short code excerpt:
(managed c++)
public delegate void my_delegate();
void del_func_cpp(vo id)
{
Console::WriteL ine("del_func_c pp called");
}
(later on..)
System::Reflect ion::Assembly^ a = cr->CompiledAssemb ly;
System::Object^ _Compiled;
_Compiled = a->CreateInstance ("test_class ");
System::Reflect ion::MethodInfo ^ mi2 =
_Compiled->GetType()->GetMethod("tes t_ff");
array<my_delega te ^^ MyArray2 = gcnew array<my_delega te ^>(1);
MyArray2[0] = gcnew my_delegate( &del_func_cp p );
MyArray2[0](); <- this still works
mi2->Invoke(_Compil ed, MyArray2); <- here it crashes
I create the c# program from the following string:
strncat_s(out, sizeof(out), "public delegate void my_delegate();\ n",
sizeof(out));
strncat_s(out, sizeof(out), "public class test_class {\n", sizeof(out));
strncat_s(out, sizeof(out), " public void test_ff(my_dele gate x)\n",
sizeof(out));
strncat_s(out, sizeof(out), " {\n", sizeof(out));
strncat_s(out, sizeof(out), " System.Console. WriteLine(\"at least the
function got called\");\n", sizeof(out));
strncat_s(out, sizeof(out), " x();\n", sizeof(out));
strncat_s(out, sizeof(out), " }\n", sizeof(out));
strncat_s(out, sizeof(out), "}", sizeof(out));
I don't get the "at least the function got called" message.