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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you, Greg. I have asked them to change the int& to int*. I am still confused by the int* PValue parameter. Here the C++ side means an array of int. if I use "[OutAttribute] int pValue" in C# side, how can I use array?
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.