protected override void DoRelease()
 {
     if (_root != null)
     {
         uint res = MAPI.MAPIFreeBuffer(_root.ptr);
         if (res != 0)
         {
             // TODO: log?
         }
         _root = null;
     }
 }
        private IntPtr AllocateRaw(int size)
        {
            IntPtr res;
            IntPtr ptr = IntPtr.Zero;

            if (_root == null)
            {
                res = MAPI.MAPIAllocateBuffer((uint)size, ref ptr);
            }
            else
            {
                res = MAPI.MAPIAllocateMore((uint)size, _root.ptr, ref ptr);
            }

            if (res != IntPtr.Zero)
            {
                throw new InvalidOperationException("MAPI Allocation failed: " + res);
            }

            // Zero it out to prevent issues
            Kernel32.ZeroMemory(ptr, size);

            return(ptr);
        }