Example #1
0
        public void Open(string name, SyncObjectTypes syncObjects)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            _name = name;

            // Open file mapping.
            _hSharedMemory = WinApi.OpenFileMapping(SectionAccessFlags.SECTION_ALL_ACCESS, false, _name);
            if (_hSharedMemory == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Error opening file mapping.");
            }

            // Get base address of file mapping.
            _pSharedMemory = WinApi.MapViewOfFile(_hSharedMemory, SectionAccessFlags.SECTION_ALL_ACCESS,
                                                  0, 0, IntPtr.Zero);
            if (_pSharedMemory == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Error getting bsae address of file mapping.");
            }

            // Create synchronisation objects.
            if (syncObjects != SyncObjectTypes.SyncObjNone)
            {
                CreateSyncObjects(syncObjects, name);
            }

            this.IsAvailable = true;
        }
Example #2
0
        public void Create(string name, int size, SyncObjectTypes syncObjects)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            _name = name;
            _size = size;

            // Create file mapping.
            _hSharedMemory = WinApi.CreateFileMapping((IntPtr)WinApi.INVALID_HANDLE_VALUE, IntPtr.Zero,
                                                      PageProtection.ReadWrite, 0, _size * Marshal.SizeOf(typeof(T)), name);
            if (_hSharedMemory == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Error creating file mapping.");
            }

            // Get base address of file mapping.
            _pSharedMemory = WinApi.MapViewOfFile(_hSharedMemory, SectionAccessFlags.SECTION_ALL_ACCESS,
                                                  0, 0, IntPtr.Zero);
            if (_pSharedMemory == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Error getting bsae address of file mapping.");
            }

            // Initialize file-mapped memory to zero.
            WinApi.ZeroMemory(_pSharedMemory, _size * Marshal.SizeOf(typeof(T)));

            // Create synchronisation objects.
            if (syncObjects != SyncObjectTypes.SyncObjNone)
            {
                CreateSyncObjects(syncObjects, name);
            }

            this.IsAvailable = true;
        }