简体   繁体   中英

C# Marshal uint*& parameter

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 is

ref IntPtr aNewIntArray

You used UIntPtr which is basically equivalent. I think you are under the impression that UIntPtr is what you use if the underlying array is unsigned but that is not the case. This is effectively a void* pointer and you will have to use Marshal.Copy to transfer to a C# array, uint[] .

Note that since the aNewIntArray really is an out parameter I think you should declare it as such

You also have declared the final parameter incorrectly. It is a 32 bit integer.

I would therefore declare the C# function like this:

method1(uint aId, out IntPtr aNewIntArray, out uint aNewIntArrayCount);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM