views:

136

answers:

0

I am working on porting some C++ code to managed .NET. I will need to retain some C++ code in native form, and trying to use an IJW approach to it. I know it's possible to declare an unmanaged struct so that it will get correctly marshaled to .NET code, but C++ compiler doesn't seem to do it.

For example I have this code (managed):

struct MyStruct
{
  int some_int;
  long some_long;
};

public ref class Class1
{
 static MyStruct GetMyStruct()
 {
  MyStruct x = { 1, 1 };
  return x;
 }
};

It compiles, but when I look at it using reflector, code looks like this:

public class Class1
{
 // Methods
 private static unsafe MyStruct GetMyStruct()
 {
  MyStruct x;
  *((int*) &x) = 1;
  *((int*) (&x + 4)) = 1;
  return x;
 }
}

[StructLayout(LayoutKind.Sequential, Size=8), NativeCppClass, 
                      MiscellaneousBits(0x41), DebugInfoInPDB]
internal struct
{
}

Basically, no fields in MyStruct are visible to .NET. Is there a way to tell C++ compiler to generate ones?

When answering, please consider this: I know how to create a managed class which could be visible to .NET framework. I am NOT interested in doing this. What I want is for the C++ compiler to declare unmanaged struct in a way that .NET will understand it, something like:

[StructLayout(LayoutKind::Sequential, blablabla ... )]
struct MyStruct
{
 [MarshalAs ....... ]
 System::Int32 some_int;
 [MarshalAs ....... ]
 System::Int32 some_long;
};