tags:

views:

55

answers:

1

My C++ DLL have a function like this:

void func1(int& count, int* pValue);

this function will determine the "count", and put some values into the pValue int array which is the length of "count".

how can I define my C# code? you can ignor the [DllImport ...] part.

thanks,

+1  A: 

By ref & isn't going to happen as far as I've worked out.

MSDN has the answers you seek

and don't forget to export your function from C++ as extern "C"

According to MSDN: Default marshaling for arrays on the C# side I think you want something like the following

public static extern void func1( 
  out int count, 
  [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] int[] values );

Where SizeParamIndex tells .net which argument will hold the size of the array to be marshaled.

Greg Domjan
5YrsLaterDBA
And also mentioned in that article: --There are cases when [OutAttribute] will be ignored. For example, [OutAttribute]int doesn't make any sense, so the [OutAttribute] is simply ignored by the CLR. The same is true of [OutAttribute] string because string is immutable.--- I cannot use [OutAttribute] with int?
5YrsLaterDBA
OK I think I can use this: [MarshalAs(UnmanagedType.LPArray)] Int32[] for those two parameters. Do I still need the [OutAttribute]? Those two parameters are really "out" type.
5YrsLaterDBA