Callback a C# method out of a managed C++ dll

  • Thread starter Sebastian Grobosch
  • Start date
S

Sebastian Grobosch

Hi All,

I have the following problem: I'm using a managed C++ Dll within a C#
project. I want to call a method of a C# class out of the Dll. I thought
about using delegates because of the managed C++ code. Could it
generally work with delegates? I tried it with this code, but no success:

managed C++ DLL:
public ref class MyClass
{
public:
delegate void CallBackFunction();

void CallTest(CallBackFunction^ Funktion);
};


C#:
// The delegate Class
public class DelegateFunctions
{
public delegate void delTest();
public delTest TestDelegate;

public DelegateFunctions()
{
}
}

....

dlgFunktion.TestDelegate = new DelegateFunctions.delTest(test);
//definition of the delegate function
....

void test()
{
MessageBox.Show("Hello World");
}

public void CallBack()
{
MyClass.CallTest(dlgFunktion.TestDelegate); // Call of the dll
function and transfer of the delegate function
}

If the method "CallBack()" is called, there should be a message box with
"Hello World", but there is only an Error CS1503 at
"MyClass.CallTest(dlgFunktion.TestDelegate)": The Argument could not be
converted from "DelegateFunctions.delTest" to "MyClass.CallBackFunction".
Any Ideas?

Sebastian
 
B

Ben Voigt [C++ MVP]

Sebastian said:
Hi All,

I have the following problem: I'm using a managed C++ Dll within a C#
project. I want to call a method of a C# class out of the Dll. I
thought about using delegates because of the managed C++ code. Could
it generally work with delegates? I tried it with this code, but no
success:
managed C++ DLL:
public ref class MyClass
{
public:
delegate void CallBackFunction();

void CallTest(CallBackFunction^ Funktion);
};


C#:
// The delegate Class
public class DelegateFunctions
{
public delegate void delTest();
public delTest TestDelegate;

public DelegateFunctions()
{
}
}

...

dlgFunktion.TestDelegate = new DelegateFunctions.delTest(test);
//definition of the delegate function
...

void test()
{
MessageBox.Show("Hello World");
}

public void CallBack()
{
MyClass.CallTest(dlgFunktion.TestDelegate); // Call of the dll
function and transfer of the delegate function
}

If the method "CallBack()" is called, there should be a message box
with "Hello World", but there is only an Error CS1503 at
"MyClass.CallTest(dlgFunktion.TestDelegate)": The Argument could not
be converted from "DelegateFunctions.delTest" to
"MyClass.CallBackFunction". Any Ideas?

Your problem has nothing to do with C++. .NET delegates are strongly-types,
so that two "compatible" delegate types with the same set of parameters and
return value and nevertheless different.

Replace
dlgFunktion.TestDelegate = new DelegateFunctions.delTest(test);
with
dlgFunktion.TestDelegate = new MyClass.CallbackFunction(test);

and everything should work (of course you must replace the declaration of
the TestDelegate field as well).
 
S

Sebastian Grobosch

Ben said:
Your problem has nothing to do with C++. .NET delegates are strongly-types,
so that two "compatible" delegate types with the same set of parameters and
return value and nevertheless different.

Replace
dlgFunktion.TestDelegate = new DelegateFunctions.delTest(test);
with
dlgFunktion.TestDelegate = new MyClass.CallbackFunction(test);

and everything should work (of course you must replace the declaration of
the TestDelegate field as well).

Thanks Ben, that works fine ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top