- Home /
Question by
nitinsharma0608 · May 02, 2012 at 08:42 AM ·
iospluginarrays
How to pass and return an array b/w c# script and c++ plugin
Hey
We want to pass an array from c# script to c++ plugin so that c++ plugin can modify that array and we can access it back in c# script. What we are doing is :
c# script :
[DllImport ("__Internal")]
private static extern void _score_global_all( ref IntPtr ptrResultVerts, ref int resultVertLength);
public int[] get_score_global_all() {
IntPtr ptrResultVerts = IntPtr.Zero;
int resultVertLength = 0;
_score_global_all(ref ptrResultVerts,ref resultVertLength);
int [] TestArray;
TestArray=new int[resultVertLength];
Marshal.Copy(ptrResultVerts
, TestArray
, 0
, resultVertLength);
return TestArray;
}
c++plugin
void _score_global_all( int* ptrResultVerts , int* resultVertLength) {
*resultVertLength = 5;
int count=*resultVertLength;
ptrResultVerts=new int[count];
for (int i = 0; i < count; i++) {
ptrResultVerts[i] = 23;
}
}
Output in c# script :
resultVertLength=5; // got it right
we get an exception "NullReferenceException" in Marshal.Copy() saying source i.e. ptrResultVerts is null.
Any Idea How to do this right ...? Any other Approach for same with sample code will be appreciated .!!!!
Thanks
Comment