Ejemplo n.º 1
0
        private SharedMemory(string name, long size)
        {
            this.name = name;
            handle    = NativeMappedFile.CreateFileMapping(NativeMappedFile.INVALID_HANDLE,
                                                           NativeMappedFile.NULL_HANDLE,
                                                           (int)NativeMappedFile.MapProtection.ReadWrite,
                                                           (int)((size >> 32) & 0xFFFFFFFF),
                                                           (int)(size & 0xFFFFFFFF), name);
            if (handle == NativeMappedFile.NULL_HANDLE)
            {
                var error = Marshal.GetHRForLastWin32Error();
                throw new IOException("CreateFileMapping returned: " + error);
            }

            long offset = 0L;

            baseAddress = NativeMappedFile.MapViewOfFile(
                handle, (int)NativeMappedFile.MapAccess.FileMapAllAccess,
                (int)((offset >> 32) & 0xFFFFFFFF),
                (int)(offset & 0xFFFFFFFF), (IntPtr)size);

            if (BaseAddress == NativeMappedFile.INVALID_HANDLE)
            {
                throw new IOException("MapViewOfFile returned: " + Marshal.GetHRForLastWin32Error());
            }
        }
Ejemplo n.º 2
0
 protected virtual void Dispose(bool disposing)
 {
     if (!isDisposed)
     {
         isDisposed = true;
         lock (taskLocker)
         {
             using (locker.Using())
             {
                 shares.Remove(name);
                 NativeMappedFile.CloseHandle(handle);
             }
         }
     }
 }