Tuesday 15 September 2015

"stack around the variable is corrupted" during c++ to c# callback -


I declare such a C ++ structure:

  straight order (long time) Replica signed); // I 8 has signed a long time; // i8 long long replica signed; // i8 signed long long id_ord; // I8 standalone status signed; // i4 signed four functions; // sign signed i1; isin_id; Four days signed i4; // i1 four values ​​[11]; // D16.5 signed amount; // i4 signed int amount_rest; // i4 signed long long id_ord1; // i8 signed in int init_amount; // i4};   

and similar C # structure:

  public structure command {public long replID; // I8 public long replica; // I8 public long replica; // I8 public long id_ord; // i8 public fulfillment status; // i4 public characters work; // i1 public int isin_id; // i4 public characters; // i1 [Marshall AS (unmanaged type., BevalArere, size console = 11)] public four [] value; // D16.5 public ent amount; // i4 public int amount_st; // i4 public long id_ord1; // i8 public int init_amount; // I4}   

I passed the composition from C ++:

  __diglex (zero) Order testing; Test.init_amount = 123; OrdersCallbackFunction (& amp; test); }   

To # c:

  OrdersCallback ordersCallback = Representative (reference command value) {Console.WriteLine ("C # command received call =" + "Init_amount =" + Value.init_amount); }; InitializeCallbacks (ordersCallback); I can read the console "init_amount = 123" which I think proves that the call works as expected and the structure is properly aligned. While debugging though I received this error:  "Run time check failure # 2 - variable  test  is corrupted around the stack."   

If I comment this line order callback function (& amp; test); Then the error has ended What's wrong with my code?

Update I forgot to mention that I have #pragma pack (push, 4) <+ / code> on the C + Div>

This possibility is not doing its packing of structs is in parity (to be able to see it in the debugger by making each of you Check the structures and allocated size)

, for "#pragma pack" C ++ and check if your struct is packed something like the following for your C # struct [StructLayout (LayoutKind.Sequential, Pack = 1)] Public Order Order {...}

In addition, as Nathan explains Your assumptions on all are closed, you want to use C ++ four or unsigned char and have maps for C ++ sbyte or byte.

I would also recommend clearly changing the structure of their struct for alignment as it thinks that you have control of both C ++ and C # side:

  Structured order {long long replication was signed; // I 8 has signed a long time; // i8 long long replica signed; // i8 signed long long id_ord; // i8 signed long long id_ord1; // I8 standalone status signed; // sign signed i4; int isin_id; // i4 signed amount; // i4 signed int amount_rest; // i4 signed int init_amount; // i4 signed four functions; // i1 signed four days; // i1 four values ​​[11]; // d16.5   

};

No comments:

Post a Comment