Royi Namir
Royi Namir

Reputation: 148714

Object sent from C++/CLI to C#?

My friend build an application ( console app) in C++/CLI.

I'm programming in C#.

Ive build for him some DLL in C# which does some calculations based on params that he send.

My function (inside my C# dll) looks like this :

bool LongCalc(dynamic obj)
{
    ...
}

(again , the obj needs to be sent from his c++/cli to my c# dll).

my question :

If he creates an object in his cpp like :

cppObj 
{
    prop1 , prop2 , prop...n
}

would I be able to read his obj in my c# as :

obj.prop1

obj.prop2 .... ?

what should he or me need to do in order for me to work with his obj ?

Upvotes: 1

Views: 250

Answers (2)

Ran
Ran

Reputation: 6159

The answer highly depends on how your friend is consuming your C# dll.

If he's using plain C++ and consuming your .NET assembly through .NET COM Interop, then I don't think a dynamic parameter will be supported for a plain C++ class like CppObj from your example, becuase there is no way to know how to invoke the object's methods during run time.

In order for this to work with a dynamic parameter in the C# side, the C++ class will probably have to be a COM object and implement IDispatch, so that the CLR will have the ability to dynamically invoke methods on the C++ object.

I'm not 100% sure that dynamic actually supports IDispatch in COM Interop, but I see no theoretical reason why it shouldn't.

Upvotes: 0

Sean
Sean

Reputation: 62532

If he's using your C# DLL from his C++ application then I'm guessing he's using managed C++. If that's the case then it'd make sense for him to define his C++ class as a managed C++ class so that you'll be able to invoke methods against it.

Upvotes: 2

Related Questions