2

I have something similar to the following in C#:

    public class ClassA
    {
        int Id  { get; set; }
        ClassB[] ClassBItems  { get; set; }    
    }  

and

    public class ClassB
    {
        int SomeOtherId {get;set;}
    }

I want to pass this object model into unmanaged C++. I.e. Have a call from the unmanaged C++ code such as 'GetClassA() : ClassA'.

So far, I have managed to pass single objects or arrays of objects from managed C# to unmanaged C++ (using COM/CCW), but haven't been pass ClassA with ClassB inside it.

My questions are:

  1. How can I pass back ClassA which a ClassB array inside it?
  2. So far, I have only been able to pass back structs from C#. My examples above are classes which is what I'd actually like to pass back. I.e. a reference to the data.

To clarify, the unmanaged code will be calling the managed code.

ahallan
  • 79
  • 6

1 Answers1

0

You can't really do that directly. You can't call C# methods from native C++ and vice versa. If you pass something, it's always just a simple C struct, with no methods.

But there are some ways to work around that:

  1. Use C++/CLI.
  2. Make COM objects out of your C# classes.

If you don't want to do either, you could try doing it by passing callbacks to the unmanaged code, or something like that. But I think it would be relatively hard and messy.

svick
  • 236,525
  • 50
  • 385
  • 514
  • I'm aware that you can't really call C# methods, but you can call into the .net code using COM, which is the same concept. I'm happy to make COM objects from my C# classes, but can I maintain the same structure mentioned in my original post? Thanks. – ahallan Feb 21 '12 at 17:28