public void CleanUpNativeData(IntPtr native_data) { var representation = new BodyParametersRepresentation(); Marshal.PtrToStructure(native_data, representation); for (int i = 0; i < representation.geopotential_size; ++i) { Marshal.DestroyStructure( representation.geopotential.At( i * Marshal.SizeOf(typeof(BodyGeopotentialElement))), typeof(BodyGeopotentialElement)); } Marshal.FreeHGlobal(representation.geopotential); Marshal.FreeHGlobal(native_data); }
public IntPtr MarshalManagedToNative(object managed_object) { var parameters = managed_object as BodyParameters; Debug.Assert(parameters != null, nameof(parameters) + " != null"); var representation = new BodyParametersRepresentation { angular_frequency = parameters.angular_frequency, axis_declination = parameters.axis_declination, axis_right_ascension = parameters.axis_right_ascension, gravitational_parameter = parameters.gravitational_parameter, j2 = parameters.j2, max_radius = parameters.max_radius, mean_radius = parameters.mean_radius, min_radius = parameters.min_radius, name = parameters.name, reference_angle = parameters.reference_angle, reference_instant = parameters.reference_instant, reference_radius = parameters.reference_radius, geopotential_size = parameters.geopotential?.Length ?? 0 }; if (representation.geopotential_size == 0) { representation.geopotential = IntPtr.Zero; } else { int sizeof_element = Marshal.SizeOf(typeof(BodyGeopotentialElement)); representation.geopotential = Marshal.AllocHGlobal( sizeof_element * parameters.geopotential.Length); for (int i = 0; i < parameters.geopotential.Length; ++i) { Marshal.StructureToPtr( parameters.geopotential[i], representation.geopotential.At(i * sizeof_element), fDeleteOld: false); } } IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(representation)); Marshal.StructureToPtr(representation, buffer, fDeleteOld: false); return(buffer); }