简体   繁体   中英

How pass a pointer to a pointer of byte array in C#

I have to call from a C# application a C function (from a DLL) declared as:

void fillBuffer( uint8_t ** pData, uint32_t * size );

This function checks if pData is not null and in case it fills the buffer with some fancy data. The variable size it is used as input parameter to limit the number of bytes that will be written in pData , and as output parameter to inform how many bytes have been actually written.

In case pData is null, it will allocate the buffer itself and will return in size the number of bytes allocated.

How I can declare and call this function from a C# application (avoiding unsafe if possibile) in both scenarios (passing an already allocated buffer or letting it allocates for me)?

I think I solved using this solution:

...
UInt32 len;
IntPtr ppData = IntPtr.Zero;

fillBuffer( ref ppData, out len );
...

And declaring the function as following:

 [DllImport( "mylib.dll", CallingConvention = CallingConvention.Cdecl )]
 static extern void fillBuffer( ref IntPtr ppData,out UInt32 size );

Thank you

You cant call C functions without unsafe context. This is simply not possible. You cant use unsafe pointers in managed context unless you declared so.

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