internal unsafe MemoryMapFile(String fileName, String fileMappingName)
        {
            //
            // Use native API to create the file directly.
            //
            SafeFileHandle fileHandle = Win32Native.UnsafeCreateFile(fileName, FileStream.GENERIC_READ, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero);
            int            lastError  = Marshal.GetLastWin32Error();

            if (fileHandle.IsInvalid)
            {
                BCLDebug.Assert(false, "Failed to create file " + fileName + ", GetLastError = " + lastError);
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidOperation_UnexpectedWin32Error"), lastError));
            }

            int highSize;
            int lowSize = Win32Native.GetFileSize(fileHandle, out highSize);

            if (lowSize == Win32Native.INVALID_FILE_SIZE)
            {
                BCLDebug.Assert(false, "Failed to get the file size of " + fileName + ", GetLastError = " + lastError);
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidOperation_UnexpectedWin32Error"), lastError));
            }

            fileSize = (((long)highSize) << 32) | ((uint)lowSize);

            if (fileSize == 0)
            {
                // we cannot map zero size file. the caller should check for the file size.
                fileHandle.Close();
                return;
            }

            SafeFileMappingHandle fileMapHandle = Win32Native.CreateFileMapping(fileHandle, IntPtr.Zero, PAGE_READONLY, 0, 0, fileMappingName);

            fileHandle.Close();
            lastError = Marshal.GetLastWin32Error();
            if (fileMapHandle.IsInvalid)
            {
                BCLDebug.Assert(false, "Failed to create file mapping for file " + fileName + ", GetLastError = " + lastError);
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidOperation_UnexpectedWin32Error"), lastError));
            }

            viewOfFileHandle = Win32Native.MapViewOfFile(fileMapHandle, SECTION_MAP_READ, 0, 0, UIntPtr.Zero);
            lastError        = Marshal.GetLastWin32Error();
            if (viewOfFileHandle.IsInvalid)
            {
                BCLDebug.Assert(false, "Failed to map a view of file " + fileName + ", GetLastError = " + lastError);
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidOperation_UnexpectedWin32Error"), lastError));
            }

            bytes = (byte *)viewOfFileHandle.DangerousGetHandle();

            fileMapHandle.Close();
        }
Пример #2
0
    /// <summary>Create a syncronized shared memory file</summary>
    /// <param name="name">The suffix of the shared memory file and semaphore.</param>
    public ProcessValueCoordinator(string name)
    {
        if (String.IsNullOrWhiteSpace(name))
        {
            throw new ArgumentNullException(name);
        }

        bool created = false;

        this.semaphore          = new Semaphore(1, 1, SemaphoreName + name, out created);
        this.sharedMemoryHandle = Win32Native.CreateFileMapping(new SafeFileHandle(IntPtr.Zero, false), IntPtr.Zero, FileMapProtection.PageReadWrite, 0, BufferSize, SharedMemoryName + name);
        this.sharedMemoryMap    = Win32Native.MapViewOfFile(this.sharedMemoryHandle, FileMapAccess.FileMapAllAccess, 0, 0, BufferSize);
    }
        private void Map()
        {
            if (m_Disposed)
            {
                // This can happen in the case where an IsolatedStorageFileStream is used after the underlying IsolatedStorageFile object is disposed.
                // which we allowed in Silverlight 3.  If this is the case, we will rebuild our state.
                Init();
                m_Disposed = false;
                GC.ReRegisterForFinalize(this);
            }

            if (m_QuotaFileMapping == null || m_QuotaFileMapping.IsInvalid)
            {
                m_QuotaFileMapping = Win32Native.CreateFileMapping(m_QuotaFileStream.SafeFileHandle, IntPtr.Zero, Win32Native.PAGE_READWRITE, 0, 0, null);

                if (m_QuotaFileMapping.IsInvalid)
                {
                    throw Marshal.GetExceptionForHR(Marshal.GetLastWin32Error());
                }
            }

            if (m_UsedSizeFileMapping == null || m_UsedSizeFileMapping.IsInvalid)
            {
                m_UsedSizeFileMapping = Win32Native.CreateFileMapping(m_UsedSizeFileStream.SafeFileHandle, IntPtr.Zero, Win32Native.PAGE_READWRITE, 0, 0, null);

                if (m_UsedSizeFileMapping.IsInvalid)
                {
                    throw Marshal.GetExceptionForHR(Marshal.GetLastWin32Error());
                }
            }

            m_QuotaView = Win32Native.MapViewOfFile(m_QuotaFileMapping, Win32Native.FILE_MAP_WRITE | Win32Native.FILE_MAP_READ, 0, 0, UIntPtr.Zero);

            if (m_QuotaView == IntPtr.Zero)
            {
                throw Marshal.GetExceptionForHR(Marshal.GetLastWin32Error());
            }

            m_UsedSizeView = Win32Native.MapViewOfFile(m_UsedSizeFileMapping, Win32Native.FILE_MAP_WRITE | Win32Native.FILE_MAP_READ, 0, 0, UIntPtr.Zero);

            if (m_UsedSizeView == IntPtr.Zero)
            {
                throw Marshal.GetExceptionForHR(Marshal.GetLastWin32Error());
            }
        }
Пример #4
0
        public override void Initialize(DebugStreamContext context)
        {
            _scope = context.GlobalScope ? @"Global\" : @"Local\";

            _logger.Information("Initializing debug listener...");

            // Initialize the security descriptor.
            _logger.Verbose("Creating new security descriptor...");
            var sd = new SecurityDescriptor();

            if (!Win32Native.InitializeSecurityDescriptor(ref sd, Win32Native.SECURITY_DESCRIPTOR_REVISION))
            {
                _logger.Error("Failed to initializes the security descriptor.");
                throw new InvalidOperationException("Failed to initializes the security descriptor.");
            }
            // Set information in a discretionary access control list
            _logger.Verbose("Creating new security descriptor dacl...");
            if (!Win32Native.SetSecurityDescriptorDacl(ref sd, true, IntPtr.Zero, false))
            {
                _logger.Error("Failed to initializes the security descriptor.");
                throw new InvalidOperationException("Failed to initializes the security descriptor.");
            }

            _logger.Verbose("Creating security attributes...");
            var sa = new SecurityAttributes();

            sa.nLength = Marshal.SizeOf(sa);
            sa.lpSecurityDescriptor = Marshal.AllocHGlobal(Marshal.SizeOf(sd));
            Marshal.StructureToPtr(sd, sa.lpSecurityDescriptor, false);

            // Create the event for slot 'DBWIN_BUFFER_READY'
            _logger.Verbose("Creating event DBWIN_BUFFER_READY...");
            _bufferReadyEvent = Win32Native.CreateEvent(ref sa, false, false, _scope + "DBWIN_BUFFER_READY");
            if (_bufferReadyEvent == IntPtr.Zero)
            {
                _logger.Error("Failed to create event DBWIN_BUFFER_READY.");
                throw new InvalidOperationException("Failed to create event 'DBWIN_BUFFER_READY'");
            }

            // Create the event for slot 'DBWIN_DATA_READY'
            _logger.Verbose("Creating event DBWIN_DATA_READY...");
            _readyEvent = Win32Native.CreateEvent(ref sa, false, false, _scope + "DBWIN_DATA_READY");
            if (_readyEvent == IntPtr.Zero)
            {
                _logger.Error("Failed to create event DBWIN_DATA_READY.");
                throw new InvalidOperationException("Failed to create event 'DBWIN_DATA_READY'");
            }

            // Get a handle to the readable shared memory at slot 'DBWIN_BUFFER'.
            _logger.Verbose("Creating file mapping to slot DBWIN_BUFFER...");
            _sharedFile = Win32Native.CreateFileMapping(new IntPtr(-1), ref sa, PageProtection.ReadWrite, 0, 4096, _scope + "DBWIN_BUFFER");
            if (_sharedFile == IntPtr.Zero)
            {
                _logger.Error("Failed to create a file mapping to slot DBWIN_BUFFER.");
                throw new InvalidOperationException("Failed to create a file mapping to slot 'DBWIN_BUFFER'");
            }

            // Create a view for this file mapping so we can access it
            _logger.Verbose("Creating mapping view to slot DBWIN_BUFFER...");
            _sharedMemory = Win32Native.MapViewOfFile(_sharedFile, Win32Native.SECTION_MAP_READ, 0, 0, 512);
            if (_sharedMemory == IntPtr.Zero)
            {
                _logger.Error("Failed to create a mapping view to slot DBWIN_BUFFER.");
                throw new InvalidOperationException("Failed to create a mapping view for slot 'DBWIN_BUFFER'");
            }

            _logger.Information("Debug listener has been initialized.");
        }