I declare such c++ structure:
struct orders
{
signed long long replID; // i8
signed long long replRev; // i8
signed long long replAct; // i8
signed long long id_ord; // i8
signed int status; // i4
signed char action; // i1
signed int isin_id; // i4
signed char dir; // i1
char price[11]; // d16.5
signed int amount; // i4
signed int amount_rest; // i4
signed long long id_ord1; // i8
signed int init_amount; // i4
};
and similar c# structure:
public struct Orders
{
public long replID; // i8
public long replRev; // i8
public long replAct; // i8
public long id_ord; // i8
public int status; // i4
public char action; // i1
public int isin_id; // i4
public char dir; // i1
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
public char[] price; // d16.5
public int amount; // i4
public int amount_rest; // i4
public long id_ord1; // i8
public int init_amount; // i4
}
I pass structure from c++:
__declspec(dllexport) void InitializeCallbacks(OrdersCallback ordersCallbackAddress_) {
OrdersCallback ordersCallbackFunction = ordersCallbackAddress_;
orders test;
test.init_amount = 123;
ordersCallbackFunction(&test);
}
To c#:
OrdersCallback ordersCallback =
delegate(ref Orders value)
{
Console.WriteLine("C# Orders call received = " +
" init_amount = " + value.init_amount);
};
InitializeCallbacks(ordersCallback);
I can read at console " init_amount = 123" what I think proves that call works as expected and structures are correctly aligned. However while debugging I receive this error: "Run-Time Check Failure #2 - Stack around the variable test
is corrupted."
If I comment this line ordersCallbackFunction(&test);
then error is gone. What's wrong with my code?
upd I forgot to mention that on c++ side I have #pragma pack(push, 4)