views:

156

answers:

0

I have a COM interface in my unmanaged C++ project that my managed C# code can find and use successfully. Currently, all the interface methods use simple type arguments like UINT and DOUBLE. What I want to do is to aggregate data into a single structure that my C# code can create and pass through the COM interface to my unmanaged C++ code. It might be useful to also go in the reverse direction, but that's not necessary at the moment. The obvious choice is to declare a C-style struct and expose it to the managed C# code, but I don't know how that would work.

Can this be done by declaring something in the .idl file and having COM interop magically transform it between the managed and unmanaged world? Assuming a C-style struct in the C++ world, what would such a structure look like in the managed C# world, some sort of value type struct object? I'm not having much luck with this approach. There's no obvious way in the VS2008 IDE to add a C-style structure declaration to my type library. It allows me to add methods and properties to an interface, but little else. Manual attempts to add one have so far failed.

If there's no way to do this automatically, how would you suggest I create and pass aggregated data, through a byte stream where I pack it in the managed code and pass it via a BSTR, to be manually unpacked on the unmanaged side? Certainly that presents challenges too, and might be a quick fix, however, it just doesn't seem right.

Thanks for any suggestions you can provide.