IJW: managed proxy structure?

I am working on porting some C ++ code to managed .NET. I will need to save the C ++ code in its native form and try to use the IJW approach to it. I know that you can declare an unmanaged structure so that it is correctly bound to the .NET code, but the C ++ compiler does not seem to do this.

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 with a reflector, the 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 the C ++ compiler to generate?

, , . , , .NET framework. . , ++ , .NET , :

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

, .NET, ++, ( "public" "value" ):

[StructLayout(LayoutKind::Sequential)]
public value struct MyStruct
{
    int some_int;
    long some_long;
};

, # C/++, . C:

struct MyUnmanagedStruct
{
         int some_int;
         long some_long;
};

, , #, :

[StructLayout(LayoutKind.Sequential)]
struct MyUnmanagedStruct
{
    public int some_int;
    public long some_long; // or as int (depends on 32/64 bitness)
};

, .NET C. StructLayout ( Pack). . : : . - PInvoke

+1

194 2 , . , . , .NET C ++. , .NET .

+1

Source: https://habr.com/ru/post/1708585/


All Articles