Passing C-style array of ints to the method in .NET dll

M

muntyanu

Hi all,
I am trying to pass array of ints to the method in .NET dll.
This is fragment of my code
// C++ side
int test[2] = new int[2] {1,2};
NETClass::DoSomething(test,2);

// .NET side
public NETClass
{
public static void DoSomething([MarshalAs(UnmanagedType.LPArray,
SizeParamIndex=1)] int[] arr, int len )
{
// use arr[0], arr[1]
}

I am getting the compile error:
cannot convert parameter 1 from 'int [2]' to 'int __gc[]'

I would greatly appreciate any comment on this,

Roman
 
W

Willy Denoyette [MVP]

Hi all,
I am trying to pass array of ints to the method in .NET dll.
This is fragment of my code
// C++ side
int test[2] = new int[2] {1,2};
NETClass::DoSomething(test,2);

// .NET side
public NETClass
{
public static void DoSomething([MarshalAs(UnmanagedType.LPArray,
SizeParamIndex=1)] int[] arr, int len )
{
// use arr[0], arr[1]
}

I am getting the compile error:
cannot convert parameter 1 from 'int [2]' to 'int __gc[]'

I would greatly appreciate any comment on this,

Roman

You can't pass an unmanaged array to managed code, declare your array as a
managed array in your managed C++ code.

System::Int32 arr[]= new int __gc[2] {.....

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top