Transfer data from c++ to c#

Hi guys,

Now I have a list of curve (ON_NurbsCurve) calculated inside c++ codes, I want to return those curve data to c#. One solution will be return the number of the curves, then return those curves one by one by a Interop function call.

Is it possible to return those curve data in one call?

Thank you.
Regards
Eric

Hi @smartunfold,

Maybe something like this?

Unmanaged:

MY_C_FUNCTION int GetMyCurvesFunction( ON_SimpleArray<ON_Curve*>* pCurveArray)
{
  int rc = 0;
  if (pCurveArray)
  {
    // TODO: Append to curve array
    // pCurveArray->Append(..);
    rc = pCurveArray->Count();
  }
  return rc;
}

Managed:

public static Curve[] GetMyCurvesFunction()
{
  using (var curves = new Rhino.Runtime.InteropWrappers.SimpleArrayCurvePointer())
  {
    var ptr_curves = curves.NonConstPointer();
    var count = UnsafeNativeMethods.GetMyCurvesFunction(ptr_curves);
    var rc = (count == 0)
      ? new Curve[0]
      : curves.ToNonConstArray();
    return rc;
  }
}

– Dale

Hi Dale,
In the c# function, the definition is : … ref IntPtr contourCrvs …
In the c++ function, the argument definition is , ON_SimpleArray<ON_Curve*>* contourCrvs …

I have added the following to the beginning of the c++ function:

ON_LineCurve* ptr = new ON_LineCurve(ON_3dPoint(0, 0, 0), ON_3dPoint(100, 0, 0));|
contourCrvs->Append(ptr);|

Here is the exception message in the c# function:
System.AccessViolationException
HResult=0x80004003
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=
StackTrace:

What can be the issue in this case?

Do you know what configuration I need to set in the c# dll project so I can debug the c++ codes as well? Now I can’t step into the c++ codes.

Thank you.

Regards
Eric

Hi @smartunfold,

Without examining your project, I have no idea.

But I’ve added my sample code to my Moose project. In C++, look for MooseGetCurves. In .NET, look for ExampleGetCurves.

Using the Debug Type drop down, on the standard VS toolbar, set the debug type to Mixed (.NET Framework). You’ll need your C++ project to be set as the “Startup Project” in order to do this.

– Dale

Hi Dale,

Thank you very much. I have put ref in front of the variable def. which causes the crash.

Regards
Eric