3
votes

I have an native C++ library which makes use of a large static buffer (it acquires data from a device).

Let's say this buffer is defined like this:

unsigned char LargeBuffer[1000000];

Now I would like to expose parts of this buffer to managed C++, e.g. when 1000 bytes of new data are stored by the library at LargeBuffer[5000] I would like to perform a callback into managed C++ code, passing a pointer to LargeBuffer[5000] so that managed C++ can access the 1000 bytes of data there (directly if possibile, i.e. without copying data, to achieve maximum performance).

What is the best way to let managed C++ code access data in this native array?

2
It's spelled "native" not "unmanaged" - Terry Mahaffey
Aren't they synonyms, in this context? - Enrico Detoma
"unmanaged" comes off as slightly pompous to native programmers - Terry Mahaffey
I don't want to be "pompous" so I changed "unmanaged" to "native" :-) - Enrico Detoma

2 Answers

4
votes

Managed C++ can access the unmanaged memory just fine. You can just pass in the pointer and use it in managed c++.

Now, if you want to then pass that data into other .NET languages, you'll need to copy that data over to managed memory structures or use unsafe code in C#

1
votes

As of .net 2.0 and the new IJW you should be able to access the buffer directly from CLI C++.

As long as you dont specify "#pragma unmanaged" then the code will be compiled as a form of managed which is what allows the direct access.