示例#1
0
        /// <summary>
        /// コンストラクタ1
        /// </summary>
        /// <param name="fileMapName">
        /// FileMapのオブジェクト名
        /// </param>
        /// <param name="maxByteSize">
        /// FileMapのバイト数(0を指定すると、全体を対象)
        /// ※ uintなので最大、4.294967295GBまで指定可能。
        /// </param>
        /// <param name="updateLockName">
        /// 更新ロックを行うMutexの名前
        /// </param>
        public SharedMemory(string fileMapName, uint maxByteSize, string updateLockName)
        {
            // メンバに設定

            // 各種設定を保持
            this._fileMapName        = fileMapName;
            this._maxFileMapByteSize = maxByteSize;
            this._updateLockName     = updateLockName;

            this._mappedViewPointer = IntPtr.Zero; // 初期化

            // FileMapを開く。
            this._fileMapHandle = MMapFileWin32.OpenFileMapping(
                MMapFileWin32.FileMapAccess.FileMapAllAccess, false, this.FileMapName);

            // 戻り値のチェック
            if (this.FileMapHandle == IntPtr.Zero)
            {
                // 開けなかった場合、

                // エラーコードをチェック
                if (CmnWin32.ErrorCodes.ERROR_FILE_NOT_FOUND == CmnWin32.GetLastError())
                {
                    // ファイルが存在しない場合、生成する。
                    this._fileMapHandle = MMapFileWin32.CreateFileMapping(
                        //IntPtr.Zero, IntPtr.Zero,
                        new IntPtr(-1), IntPtr.Zero,
                        MMapFileWin32.FileMapProtection.PageReadWrite
                        | MMapFileWin32.FileMapProtection.SectionCommit,
                        0, this.MaxFileMapByteSize, this.FileMapName);
                }
                else
                {
                    // 戻り値のチェック
                    if (this.FileMapHandle == IntPtr.Zero)
                    {
                        // 生成できなかった場合

                        this.Dispose(); // GC前にクリーンナップ

                        throw new WindowsAPIErrorException(
                                  CmnWin32.GetLastError(), string.Format(
                                      WindowsAPIErrorException.MessageTemplate, "CreateFileMapping"));
                    }
                    else
                    {
                        // 生成できた場合
                    }
                }
            }
            else
            {
                // 開けた場合
            }
        }
示例#2
0
        /// <summary>
        /// FileMapをメモリ空間にマップし、MapViewを取得する。
        /// </summary>
        /// <param name="offset">
        /// FileMapの下位オフセット(32bitに制限)
        /// ※ uintなので最大、4.294967295GBまで指定可能。
        /// </param>
        /// <param name="mapViewByteSize">
        /// FileMapのバイト数(0を指定すると、全体を対象)
        /// ※ uintなので最大、4.294967295GBまで指定可能。
        /// </param>
        public void Map(uint offset, uint mapViewByteSize)
        {
            // チェック
            if (this.IsDisposed)
            {
                throw new ObjectDisposedException("SharedMemory");//, "Dispose済み。");
            }

            // offsetHighは設定しない(32bitに制限するため)。
            uint offsetHigh = 0;
            uint offsetLow  = offset;

            // マイナス値や、FileMapのサイズを超える場合は、FileMapのサイズに合わせる
            if (mapViewByteSize < 0 || (this.MaxFileMapByteSize < mapViewByteSize))
            {
                this._currentMapViewByteSize = this.MaxFileMapByteSize;
            }

            // 既にマップされている場合は、
            if (this._mappedViewPointer != IntPtr.Zero)
            {
                // 一度アンマップしてから、
                this.Unmap();
            }
            // マップしなおす(↓)。

            // FileMapをメモリ空間にマップし、
            // MapViewを取得する(MapViewのアドレスを返す)。
            this._mappedViewPointer
                = MMapFileWin32.MapViewOfFile(this.FileMapHandle,
                                              MMapFileWin32.FileMapAccess.FileMapAllAccess,
                                              offsetHigh, offsetLow, this.CurrentMapViewByteSize);

            // 0を指定した際の仕様に合わせて
            if (this._currentMapViewByteSize == 0)
            {
                this._currentMapViewByteSize = this.MaxFileMapByteSize;
            }

            // MapViewの取得エラー
            if (this.MappedViewPointer == IntPtr.Zero)
            {
                this.Dispose(); // GC前にクリーンナップ

                throw new WindowsAPIErrorException(
                          CmnWin32.GetLastError(), string.Format(
                              WindowsAPIErrorException.MessageTemplate, "MapViewOfFile"));
            }
        }
示例#3
0
        /// <summary>MapViewをメモリ空間からアンマップする。</summary>
        public void Unmap()
        {
            // チェック
            if (this.IsDisposed)
            {
                throw new ObjectDisposedException("SharedMemory");//, "Dispose済み。");
            }

            // MapViewが存在しない場合は処理しない
            if (this.MappedViewPointer != IntPtr.Zero)
            {
                // UnmapViewOfFileでアンマップ
                MMapFileWin32.UnmapViewOfFile(this.MappedViewPointer);

                // IntPtr.Zeroでクリア
                this._mappedViewPointer = IntPtr.Zero;
            }
        }