1

We have a collection of Native C++ business rules wrapped in C++/CLI and then by a C# layer so we can expose it via DCOM.

The Native C++ business rules are in a DLL and are easily invoked in C++/CLI which is in a COM visible DLL. We then add this DLL as a reference to the C# project.

The C# layer has a lot of classes derived from ServicedComponent and we apply regsvcs to get it registered as a DCOM server.

However a need has arisen for the C++/CLI layer to communicate to the C# layer. To do this I was thinking of using a C# style interface. The C# layer will derive from the interface and pass it down to the C++/CLI layer which can then call methods on it to get something to occur in the C# layer. This interface will need to be declared in the C++/CLI layer though for visibility. This is my problem I do not seem to be able to achieve this.

Is there a way to declare a C# style interface in C++/CLI and then use it in C#?

2
  • "Managed C++" refers to Visual C++ with Managed Extensions, as in Visual Studio .NET 2003. You probably mean its successor C++/CLI. If so, please reword and changed the tags. Commented May 21, 2015 at 23:02
  • Changed as suggested. Commented May 21, 2015 at 23:38

1 Answer 1

2

There is (https://msdn.microsoft.com/en-us/library/737cydt1.aspx):

#pragma managed
using namespace System;

public interface class MyInterface
{
    void MyFunction();
}

And used in C#:

public class MyClass : MyInterface
{
    public void MyFunction() { }
}
Sign up to request clarification or add additional context in comments.

9 Comments

If I declare it like that I get. error CS0246: The type or namespace name 'MyInterface' could not be found (are you missing a using directive or an assembly reference?)
BTW The assembly and using statement are there. If I remove the method MyFunction from the class deriving off MyInterface I get Intellisense errors that "Interface member xxx is not implemented"
I edit the answer to show exactly what I've compiled without error. I'm using VS2013.
Okay but how is a C++/CLI Interface used in a C# assembly without error. I get errors for some reason.
You need a class to implement the interface.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.