예제 #1
0
        public Segment(string name, SharedMemoryCreationFlag creationFlag, int size)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new SharedMemoryException("You must supply a segment name");
            }

            if (size <= 0 && creationFlag == SharedMemoryCreationFlag.Create)
            {
                throw new SharedMemoryException("Size must be postive and non-zero");
            }

            // Create unique named mutex
            guard = new Mutex(false, "m" + name);

            // Create or attach to shared memory segment
            if (creationFlag == SharedMemoryCreationFlag.Create)
            {
                nativeHandle = Win32Native.CreateFileMapping((IntPtr)Win32Native.INVALID_HANDLE_VALUE,
                                                             IntPtr.Zero,
                                                             Win32Native.ProtectionLevel.PAGE_READWRITE,
                                                             0,
                                                             size,
                                                             name);
            }
            else
            {
                nativeHandle = Win32Native.OpenFileMapping(Win32Native.FileMap.FILE_MAP_ALL_ACCESS, true, name);
            }

            if (nativeHandle == IntPtr.Zero)
            {
                uint i = Win32Native.GetLastError();
                if (i == Win32Native.ERROR_INVALID_HANDLE)
                {
                    throw new SharedMemoryException("Shared memory segment already in use");
                }
                else
                {
                    throw new SharedMemoryException("Unable to access shared memory segment. GetLastError = " + i);
                }
            }

            // Get pointer to shared memory segment
            nativePointer = Win32Native.MapViewOfFile(nativeHandle, Win32Native.FileMap.FILE_MAP_ALL_ACCESS, 0, 0, 0);

            if (nativePointer == IntPtr.Zero)
            {
                uint i = Win32Native.GetLastError();
                Win32Native.CloseHandle(nativeHandle);
                nativeHandle = IntPtr.Zero;
                throw new SharedMemoryException("Unable to map shared memory segment. GetLastError = " + i);
            }

            this.currentSize = size;
        }
예제 #2
0
파일: Segment.cs 프로젝트: paladin74/Dapple
        public Segment(string name, SharedMemoryCreationFlag creationFlag, int size)
        {
            if (String.IsNullOrEmpty(name))
             {
            throw new SharedMemoryException("You must supply a segment name");
             }

             if (size <= 0 && creationFlag == SharedMemoryCreationFlag.Create)
             {
            throw new SharedMemoryException("Size must be postive and non-zero");
             }

             // Create unique named mutex
             guard = new Mutex(false, name + "Mutex");

             // Create or attach to shared memory segment
             if (creationFlag == SharedMemoryCreationFlag.Create)
             {
            nativeHandle = Win32Native.CreateFileMapping((IntPtr)Win32Native.INVALID_HANDLE_VALUE,
               IntPtr.Zero,
               Win32Native.ProtectionLevel.PAGE_READWRITE,
               0,
               size,
               name);
             }
             else
             {
            nativeHandle = Win32Native.OpenFileMapping(Win32Native.FileMap.FILE_MAP_ALL_ACCESS, true, name);
             }

             if (nativeHandle == IntPtr.Zero)
             {
                int i = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
            if (i == Win32Native.ERROR_INVALID_HANDLE)
               throw new SharedMemoryException("Shared memory segment already in use");
            else
               throw new SharedMemoryException("Unable to access shared memory segment. GetLastError = " + i);
             }

             // Get pointer to shared memory segment
             nativePointer = Win32Native.MapViewOfFile(nativeHandle, Win32Native.FileMap.FILE_MAP_ALL_ACCESS, 0, 0, 0);

             if (nativePointer == IntPtr.Zero)
             {
                int i = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
            Win32Native.CloseHandle(nativeHandle);
            nativeHandle = IntPtr.Zero;
            throw new SharedMemoryException("Unable to map shared memory segment. GetLastError = " + i);
             }

             this.currentSize = size;
        }