A
Andreas Reiff
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 CSharpCodeProvider).
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.ArgumentException' 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(void)
{
Console::WriteLine("del_func_cpp called");
}
(later on..)
System::Reflection::Assembly^ a = cr->CompiledAssembly;
System::Object^ _Compiled;
_Compiled = a->CreateInstance("test_class");
System::Reflection::MethodInfo^ mi2 =
_Compiled->GetType()->GetMethod("test_ff");
array<my_delegate ^> ^ MyArray2 = gcnew array<my_delegate ^>(1);
MyArray2[0] = gcnew my_delegate( &del_func_cpp );
MyArray2[0](); <- this still works
mi2->Invoke(_Compiled, 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_delegate 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.
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 CSharpCodeProvider).
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.ArgumentException' 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(void)
{
Console::WriteLine("del_func_cpp called");
}
(later on..)
System::Reflection::Assembly^ a = cr->CompiledAssembly;
System::Object^ _Compiled;
_Compiled = a->CreateInstance("test_class");
System::Reflection::MethodInfo^ mi2 =
_Compiled->GetType()->GetMethod("test_ff");
array<my_delegate ^> ^ MyArray2 = gcnew array<my_delegate ^>(1);
MyArray2[0] = gcnew my_delegate( &del_func_cpp );
MyArray2[0](); <- this still works
mi2->Invoke(_Compiled, 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_delegate 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.