I’M browsing through the whole stackoverflow forum but I’m quite unsure if my marshalling solution fits to my problem.
I got a c++ method returning an array of integer via a parameter. The prototype is the following:
method1(uint aId, uint*& aNewIntArray, uint& aNewIntArrayCount);
I marshal the parameters like:
method1(UInt32 aId, ref UIntPtr aNewIntArray, ref UInt64 aNewIntArrayCount);
I marshal uint*& to ref UIntPtr but I’m not very sure if this is correct and while i not found another one having the same problem i’ll ask on myself.
Another idea is: Is it possible to marshal int* and int& parameter the same way using
ref UInt32
? Or did I need to use UIntPtr / IntPtr without a “ref” Keyword?
In this case I would prefer to use ref instead of out to avoid the C++ uses a not initialized int reference.
Your C++ method is allocating an array of integers and returning a pointer to the first element in
aNewIntArray. Therefore the match C# definition for that parameter isYou used
UIntPtrwhich is basically equivalent. I think you are under the impression thatUIntPtris what you use if the underlying array is unsigned but that is not the case. This is effectively avoid*pointer and you will have to useMarshal.Copyto transfer to a C# array,uint[].Note that since the
aNewIntArrayreally is an out parameter I think you should declare it as suchYou also have declared the final parameter incorrectly. It is a 32 bit integer.
I would therefore declare the C# function like this: