As others have mentioned, there is no way you can store the data in a managed byte[]
without copying (with the current structure you've provided*). However, if you don't actually need it to be in a managed buffer, you can use unsafe
operations to work directly with the unmanaged memory. It really depends what you need to do with it.
All byte[]
and other reference types are managed by the CLR Garbage Collector, and this is what is responsible for allocation of memory and deallocation when it is no longer used. The memory pointed to by the return of GetBuffer
is a block of unmanaged memory allocated by the C++ code and (memory layout / implementation details aside) is essentially completely separate to your GC managed memory. Therefore, if you want to use a GC managed CLR type (byte[]
) to contain all the data currently held within your unmanaged memory pointed to by your IntPtr
, it needs to be moved (copied) into memory that the GC knows about. This can be done by Marshal.Copy
or by a custom method using unsafe
code or pinvoke or what have you.
However, it depends what you want to do with it. You've mentioned it's video data. If you want to apply some transform or filter to the data, you can probably do it directly on the unmanaged buffer. If you want to save the buffer to disk, you can probably do it directly on the unmanaged buffer.
On the topic of length, there is no way to know the length of an unmanaged memory buffer unless the function that allocated the buffer also tells you what the length is. This can be done in lots of ways, as commenters have mentioned (first field of the structure, out paramtere on the method).
*Finally, if you have control of the C++ code it might be possible to modify it so that it is not responsible for allocating the buffer it writes the data to, and instead is provided with a pointer to a preallocated buffer. You could then create a managed byte[]
in C#, preallocated to the size required by your C++ code, and use the GCHandle
type to pin it and provide the pointer to your C++ code.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…