x


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 scoreglobal_all( ref IntPtr ptrResultVerts, ref int resultVertLength);

public int[] getscoreglobal_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 scoreglobal_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

more ▼

asked May 02, 2012 at 08:42 AM

nitinsharma0608 gravatar image

nitinsharma0608
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

By RSS:

Answers

Answers and Comments

Topics:

x2782
x1858
x694
x618

asked: May 02, 2012 at 08:42 AM

Seen: 1253 times

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