Your p/invoke is incorrect. It should, perhaps, read:
[DllImport("BSRobots20.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern uint BS_Robots_Connect(
out int nRobotCount,
[Out] int[] pnRobotIDS,
[Out] int[] pnRobotTypes,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U1)]
[Out] bool[] pbRobotReadys
);
It is plausible that nRobotCount
should be ref
rather than out
. Determine that based on whether or not you need to pass information into the function. And are you sure that pbRobotReadys
is an array? If not then use out bool
or ref bool
.
So perhaps what you need is:
[DllImport("BSRobots20.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern uint BS_Robots_Connect(
ref int nRobotCount,
[Out] int[] pnRobotIDS,
[Out] int[] pnRobotTypes,
[MarshalAs(UnmanagedType.U1)]
out bool pbRobotReadys
);
To get to the bottom of this you'll need to study the documentation a little more, and perhaps refer to any C++ example code that you can find.
You will need to allocate arrays before calling the function. I cannot tell from here how exactly you need to allocate the arrays. Presumably you know how large they need to be.
Why was your version wrong? Well, the way to think about this is that a C# array is already a reference. By passing the array using out
you are passing a pointer to a pointer. In other words, one level of indirection too far.
Another way to think about this is that you were asking the unmanaged code to create managed arrays. And that is something that it manifestly cannot do.