Quantcast
Channel: Rhino Developer - McNeel Forum
Viewing all articles
Browse latest Browse all 8532

Pointer (array of int) from C++ to C#

$
0
0

@Petras_Vestartas wrote:

Hi,

I am learning Rhino plugin development when wrapping C++ classes to pass data to C#.

I have question about pointers.

In C++ I have a class that has a getter of "int* values", which is just array of integers.
I class works and I can return integer one by one from class, but do not know how to pass whole array.
But every array in C++ is a pointer. So how to return values associated with a pointer (array of integers) to C#?

I tried this when passing C++ function to C#, but ok pointers do not exist in C# and I wonder if int* is correct:

SAMPLELIBRARY_C_FUNCTION int* colors(int x) {
	MyGraph g(x);
	g.addEdge(0, 1);
	g.addEdge(0, 2);
	g.addEdge(1, 2);
	g.addEdge(1, 3);
	g.addEdge(2, 3);
	g.addEdge(3, 4);
	g.greedyColoring();
	return g.getValues();
}

What is a correct way to pass int* colors function in C# in UnsafeNativeMethods.cs to get integer array?
For now I tried:

[DllImport(Import.lib, CallingConvention = CallingConvention.Cdecl)]
internal static extern int[] colors(int x);

and then I print array values:
int[] values = UnsafeNativeMethods.colors(5);
for(int i = 0; i < values.Length; i++)
Rhino.RhinoApp.WriteLine(values[i].ToString());

But try catch block gives me an exception in Rhino:
Cannot marshal 'return value': Invalid managed/unmanaged type combination.

Posts: 6

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 8532

Trending Articles