Before updating to Windows 10 you may need to return your Pro license. More information here

How to pass and return an array b/w c# script and c++ plugin


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];
             , TestArray
             , 0
             , resultVertLength);
        return TestArray;


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 .!!!!


more ▼

asked May 02, 2012 at 08:42 AM

avatar image

0 6 4 4

Is the question relevant to Unity3D?

May 02, 2012 at 08:47 AM venkspower
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: May 02, 2012 at 08:42 AM

Seen: 1405 times

Last Updated: May 02, 2012 at 08:47 AM