0

I have 2 arrays in C# and one API in c++ (provided library, no source)

public struct POS_DATA_2D
{
    public UInt32 u32_opt;
    public Int32 i32_x;
    public Int32 i32_y;
    public Int32 i32_theta;
}
public struct PNT_DATA_2D
{
    public UInt32 u32_opt;
    public Int32 i32_x;
    public Int32 i32_y;
    public Int32 i32_theta;
    public Int32 i32_acc;
    public Int32 i32_dec;
    public Int32 i32_vi; 
    public Int32 i32_vm;
    public Int32 i32_ve;
}
int cnt = 58;
POS_DATA_2D[] pPosarr = new POS_DATA_2D[cnt];
PNT_DATA_2D[] pPntarr = new PNT_DATA_2D[cnt];

The problem is when I call the below API the program crashed. I guessed it accessed array element which isn't correct.

ADCNC_W32.APS168.VelocityPlanning(cnt, ref pPosarr[0], ref pPntarr[0]);

The description of that API

 [DllImport("ADCNC.dll")]public static extern Int32 
VelocityPlanning(
    System.UInt32 PosCount    // [IN ] position array count, [1,4294967295]
 ,  ref POS_DATA_2D PosArray    // [IN ] position array start pointer, [!NULL]
 ,  ref PNT_DATA_2D PntArray    // [OUT] point array start pointer, [!NULL]
 );

Please help me to point out the incorrect points. Thanks!

5
  • possible duplicate of How to marshal an array of structure In C#? Commented Feb 4, 2015 at 16:20
  • Can you post the C method declaration so we can check if the DllImport is written correctly? Commented Feb 4, 2015 at 16:24
  • This is structure in C++ I32 __stdcall VelocityPlanning( U32 PosCount , POS_DATA_2DPosArray, PNT_DATA_2D PntArray ); Commented Feb 4, 2015 at 17:12
  • That C++ doesn't look like it is accepting an array. It looks like it is accepting a single POS_DATA_2D object, passed by value on the stack. But your C# code clearly is passing a pointer to an array element. I can't tell entirely without seeing the definition of POS_DATA_2D in the C++ code. Can you post that too? Also: Make sure you check the structure size and alignment. Commented Feb 5, 2015 at 21:39
  • This is truct of POS_DATA_2D public struct POS_DATA_2D { public UInt32 u32_opt; public Int32 i32_x; public Int32 i32_y; public Int32 i32_theta; } Commented Feb 10, 2015 at 13:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.