/// <summary>
        /// Creates shared memory object.
        /// </summary>
        public void LowLevel_CreateSharedMemory()
        {
            if (WindowsAPI.FailureReason != null)
            {
                throw OperationException.WrapException(WindowsAPI.FailureReason);
            }

            IParameterProvider parameters      = this.ITransportContext.IParameterProvider;
            string             fileMappingName = GenuineSharedMemoryChannel.ConstructSharedObjectName(
                this.ShareName, parameters);

            this._mapHandle = WindowsAPI.CreateFileMapping((IntPtr)(int)-1, WindowsAPI.AttributesWithNullDACL, WindowsAPI.PAGE_READWRITE, 0, (uint)this._shareSize, fileMappingName);
            if (this._mapHandle == IntPtr.Zero)
            {
                throw GenuineExceptions.Get_Windows_CanNotCreateOrOpenSharedMemory(Marshal.GetLastWin32Error());
            }

            this._pointer = WindowsAPI.MapViewOfFile(this._mapHandle, WindowsAPI.SECTION_MAP_READ | WindowsAPI.SECTION_MAP_WRITE, 0, 0, 0);
            if (this._pointer == IntPtr.Zero)
            {
                int lastWinError = Marshal.GetLastWin32Error();
                WindowsAPI.CloseHandle(this._mapHandle);
                throw GenuineExceptions.Get_Windows_SharedMemoryError(lastWinError);
            }
        }
//		/// <summary>
//		/// Creates a new mutex with NULL DACL.
//		/// </summary>
//		/// <param name="mutex">The mutex.</param>
//		/// <param name="name">The name of the mutex.</param>
//		static internal void UpgrageMutexSecurity(Mutex mutex, string name)
//		{
//			CloseHandle(mutex.Handle);
//			mutex.Handle = CreateMutex(AttributesWithNullDACL, false, name);
//		}

        /// <summary>
        /// Opens an existent mutex.
        /// </summary>
        /// <param name="name">The name of the mutex.</param>
        /// <returns>The opened mutex.</returns>
        static internal Mutex OpenMutex(string name)
        {
            IntPtr result = OpenMutex(MUTEX_ALL_ACCESS, false, name);

            if (result == IntPtr.Zero)
            {
                throw GenuineExceptions.Get_Windows_SharedMemoryError(Marshal.GetLastWin32Error());
            }

            Mutex mutex = new Mutex();

            mutex.SafeWaitHandle = new SafeWaitHandle(result, true);

            return(mutex);
        }
        static internal Mutex CreateMutex(string name)
        {
            IntPtr result = CreateMutex(AttributesWithNullDACL, false, name);

            if (result == IntPtr.Zero)
            {
                throw GenuineExceptions.Get_Windows_SharedMemoryError(Marshal.GetLastWin32Error());
            }

            if (Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS)
            {
                throw GenuineExceptions.Get_Windows_SharedMemoryError(Marshal.GetLastWin32Error());
            }

            Mutex mutex = new Mutex();

            mutex.SafeWaitHandle = new SafeWaitHandle(result, true);

            return(mutex);
        }
        /// <summary>
        /// Opens the handle of an existent share.
        /// </summary>
        public void OpenSharedMemory()
        {
            IParameterProvider parameters      = this.ITransportContext.IParameterProvider;
            string             fileMappingName = GenuineSharedMemoryChannel.ConstructSharedObjectName(
                this.ShareName, parameters);

            this._mapHandle = WindowsAPI.OpenFileMapping(WindowsAPI.FILE_MAP_ALL_ACCESS, 0, fileMappingName);
            if (this._mapHandle == IntPtr.Zero)
            {
                throw GenuineExceptions.Get_Windows_CanNotCreateOrOpenSharedMemory(Marshal.GetLastWin32Error());
            }

            this._pointer = WindowsAPI.MapViewOfFile(this._mapHandle, WindowsAPI.SECTION_MAP_READ | WindowsAPI.SECTION_MAP_WRITE, 0, 0, 0);
            if (this._pointer == IntPtr.Zero)
            {
                int lastWinError = Marshal.GetLastWin32Error();
                WindowsAPI.CloseHandle(this._mapHandle);
                throw GenuineExceptions.Get_Windows_SharedMemoryError(lastWinError);
            }
        }
        static WindowsAPI()
        {
            NullAttribute = Marshal.AllocHGlobal(SECURITY_DESCRIPTOR_MIN_LENGTH);

            if (NullAttribute == IntPtr.Zero)
            {
                return;
            }

            try
            {
                if (!InitializeSecurityDescriptor(NullAttribute, SECURITY_DESCRIPTOR_REVISION))
                {
                    throw GenuineExceptions.Get_Windows_SharedMemoryError(Marshal.GetLastWin32Error());
                }

                if (!SetSecurityDescriptorDacl(NullAttribute, true, IntPtr.Zero, false))
                {
                    throw GenuineExceptions.Get_Windows_SharedMemoryError(Marshal.GetLastWin32Error());
                }

                AttributesWithNullDACL         = new _SECURITY_ATTRIBUTES();
                AttributesWithNullDACL.nLength = 12;
                AttributesWithNullDACL.lpSecurityDescriptor = NullAttribute;
                AttributesWithNullDACL.bInheritHandle       = 1;
            }
            catch (Exception ex)
            {
                FailureReason = ex;
                try
                {
                    Marshal.FreeHGlobal(NullAttribute);
                }
                catch (Exception)
                {
                }

                NullAttribute = IntPtr.Zero;
            }
        }