Exemple #1
0
            /// <summary>
            ///
            /// </summary>
            /// <param name="fileName"></param>
            /// <param name="protection"></param>
            /// <param name="maxSize"></param>
            /// <param name="name"></param>
            public void Create(String fileName, MapProtection protection,
                               long maxSize, String name)
            {
                // open file first
                IntPtr hFile = INVALID_HANDLE_VALUE;

                try
                {
                    if (fileName != null)
                    {
                        // determine file access needed
                        // we'll always need generic read access
                        int desiredAccess = GENERIC_READ;
                        if ((protection == MapProtection.PageReadWrite) ||
                            (protection == MapProtection.PageWriteCopy))
                        {
                            desiredAccess |= GENERIC_WRITE;
                        }

                        // open or create the file
                        // if it doesn't exist, it gets created
                        hFile = Win32MapApis.CreateFile(
                            GetMMFDir() + fileName, desiredAccess, 0,
                            IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero
                            );
                        if (hFile != NULL_HANDLE)
                        {
                            m_hMap = Win32MapApis.CreateFileMapping(
                                hFile, IntPtr.Zero, (int)protection,
                                (int)((maxSize >> 32) & 0xFFFFFFFF),
                                (int)(maxSize & 0xFFFFFFFF), name
                                );
                        }
                        else
                        {
                            throw new FileMapIOException(Marshal.GetHRForLastWin32Error());
                        }
                        if (m_hMap == NULL_HANDLE)
                        {
                            throw new FileMapIOException(Marshal.GetHRForLastWin32Error());
                        }
                    }
                }
                catch (Exception Err)
                {
                    throw Err;
                }
                finally
                {
                    if (!(hFile == NULL_HANDLE))
                    {
                        Win32MapApis.CloseHandle(hFile);
                    }
                }
            }
Exemple #2
0
            /// <summary>
            ///
            /// </summary>
            /// <param name="fileName"></param>
            /// <param name="protection"></param>
            /// <param name="access"></param>
            /// <param name="maxSize"></param>
            /// <param name="name"></param>
            public MemoryMappedFile(String fileName, MapProtection protection, MapAccess access,
                                    long maxSize, String name)
            {
                //try to open the mmf by name. if failed create the file and mmf.
                IntPtr hFile = IntPtr.Zero;

                try
                {
                    m_hMap = Win32MapApis.OpenFileMapping((int)access, false, name);
                    if (m_hMap == NULL_HANDLE)
                    {
                        int desiredAccess = GENERIC_READ;
                        if ((protection == MapProtection.PageReadWrite) ||
                            (protection == MapProtection.PageWriteCopy))
                        {
                            desiredAccess |= GENERIC_WRITE;
                        }
                        hFile = Win32MapApis.CreateFile(
                            GetMMFDir() + fileName, desiredAccess, 0,
                            IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero
                            );
                        if (hFile != NULL_HANDLE)
                        {
                            m_hMap = Win32MapApis.CreateFileMapping(
                                hFile, IntPtr.Zero, (int)protection,
                                0,
                                (int)(maxSize & 0xFFFFFFFF), name
                                );
                            if (m_hMap != NULL_HANDLE)
                            {
                                m_maxSize = maxSize;
                            }
                            else
                            {
                                throw new FileMapIOException(Marshal.GetHRForLastWin32Error());
                            }
                        }
                        else
                        {
                            throw new FileMapIOException(Marshal.GetHRForLastWin32Error());
                        }
                    }
                }
                catch (Exception Err)
                {
                    throw Err;
                }
                finally
                {
                    if (!(hFile == NULL_HANDLE))
                    {
                        Win32MapApis.CloseHandle(hFile);
                    }
                }
            }