Friday, March 24, 2006

Passing arrays from C# to Managed C++ : 2

After some more research, here is the best way to define the Managed C++ functions, so that you can pass them from C# easily. /* pass by reference Call from C# like this double dataValue = 0; mClassObj.ValueByRef(ref dataValue); */ void ValueByRef ([In][Out] System::Double *data) //instead of &data { *data = 100; } /* returning an array Call from C# like this double []dataArrayReturned = mClassObj.ReturnArray(); */ System::Double ReturnArray()[] { System::Double data[] = new System::Double[3]; data[0] = 3; data[1] = 33; data[2] = 333; return data; } /* pass as an out param Call from C# like this double []dataArrayAsOut; mClassObj.CreateArrayOut(out dataArrayAsOut); */ void CreateArrayOut([Out] System::Double (*data)[]) { *data = new System::Double[3]; System::Double tmp[] = *data; tmp[0] = 1; tmp[1] = 11; tmp[2] = 111; } /* pass as a ref param Call from C# like this double []dataArrayByRef = null; mClassObj.CreateArrayRef(ref dataArrayByRef); */ void CreateArrayRef ([In][Out] System::Double (*data)[]) //System::Double (&data)[]{ *data = new System::Double[3]; System::Double tmp[] = *data; tmp[0] = 2; tmp[1] = 22; tmp[2] = 222; }

No comments:

Post a Comment

Remember, if you want me to respond to your comment, then you need to use a Google/OpenID account to leave the comment.