Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a SafeBuffer in C#

Tags:

c#

buffer

What is a simple example of how to use SafebUffer? That is, how do I create and initialise one? MSDN documentation don't appear to show this.

like image 439
ManInMoon Avatar asked Sep 06 '10 14:09

ManInMoon


1 Answers

You can't, it is an abstract class. The only visible concrete implementation of it is SafeMemoryMappedViewHandle, a helper class for the classes in the System.IO.MemoryMappedFiles namespace. It has a non-accessible constructor since it can only be properly initialized by the plumbing that makes memory mapped files work.

The use case is an IntPtr that maps to unmanaged memory, managed by a handle. Fairly rare in the Windows API, MapViewOfFile or GlobalAllocPtr for example. If you want to create your own then you have to derive from SafeBuffer so you can call its constructor and call, say, AcquirePointer. Most of this is unsafe. What are you really trying to do?

like image 119
Hans Passant Avatar answered Oct 21 '22 14:10

Hans Passant