⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sharememory.cs

📁 破解的飞信源代码
💻 CS
字号:
namespace Imps.Client.Utils
{
    using System;
    using System.Runtime.InteropServices;

    public class ShareMemory : IDisposable
    {
        private IntPtr _hFileMap = IntPtr.Zero;
        private IntPtr _hMemory = IntPtr.Zero;
        private int _size = 0;
        private const int ERROR_ALREADY_EXISTS = 0xb7;
        private const int FILE_MAP_ALL_ACCESS = 6;
        private const int FILE_MAP_COPY = 1;
        private const int FILE_MAP_READ = 4;
        private const int FILE_MAP_WRITE = 2;
        private static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
        private bool isDisposed;
        private const int PAGE_EXECUTE = 0x10;
        private const int PAGE_EXECUTE_READ = 0x20;
        private const int PAGE_EXECUTE_READWRITE = 0x40;
        private const int PAGE_READONLY = 2;
        private const int PAGE_READWRITE = 4;
        private const int PAGE_WRITECOPY = 8;
        private const int SEC_COMMIT = 0x8000000;
        private const int SEC_IMAGE = 0x1000000;
        private const int SEC_NOCACHE = 0x10000000;
        private const int SEC_RESERVE = 0x4000000;

        private ShareMemory()
        {
        }

        [DllImport("Kernel32.dll")]
        private static extern bool CloseHandle(IntPtr handle);
        public static ShareMemory Create(string name, int size, bool read)
        {
            ShareMemory memory = new ShareMemory();
            if (!memory.InnerCreate(name, size, read))
            {
                return null;
            }
            return memory;
        }

        [DllImport("Kernel32.dll")]
        private static extern IntPtr CreateFileMapping(IntPtr hFile, IntPtr lpAttributes, int flProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, string lpName);
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.isDisposed)
            {
                UnmapViewOfFile(this._hMemory);
                CloseHandle(this._hFileMap);
            }
            this.isDisposed = true;
        }

        ~ShareMemory()
        {
            this.Dispose(false);
        }

        [DllImport("kernel32")]
        private static extern int GetLastError();
        [DllImport("advapi32.dll", EntryPoint="GetSecurityInfo", CharSet=CharSet.Unicode, SetLastError=true)]
        internal static extern uint GetSecurityInfoByHandle(SafeHandle handle, uint objectType, uint securityInformation, out IntPtr sidOwner, out IntPtr sidGroup, out IntPtr dacl, out IntPtr sacl, out IntPtr securityDescriptor);
        private bool InnerCreate(string name, int size, bool read)
        {
            new SecurityStruct();
            IntPtr lpAttributes = IntPtr.Zero;
            this._hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE, lpAttributes, 4, 0, size, name);
            if (this._hFileMap == IntPtr.Zero)
            {
                return false;
            }
            if (GetLastError() == 0xb7)
            {
                if (!read)
                {
                    return false;
                }
                this._hMemory = MapViewOfFile(this._hFileMap, 4, 0, 0, 0);
            }
            else if (!read)
            {
                this._hMemory = MapViewOfFile(this._hFileMap, 2, 0, 0, 0);
                this._size = size;
            }
            else
            {
                return false;
            }
            if (this._hMemory == IntPtr.Zero)
            {
                return false;
            }
            return true;
        }

        [DllImport("Kernel32.dll")]
        private static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, int dwNumberOfBytesToMap);
        [DllImport("Kernel32.dll")]
        private static extern IntPtr OpenFileMapping(int dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, string lpName);
        public int Read(byte[] buf, int offset, int size)
        {
            int length = Marshal.ReadInt32(this._hMemory);
            if ((length > 0) && (length <= size))
            {
                IntPtr source = new IntPtr(this._hMemory.ToInt32() + 4);
                Marshal.Copy(source, buf, offset, length);
            }
            return length;
        }

        [DllImport("Kernel32.dll")]
        private static extern bool UnmapViewOfFile(IntPtr pvBaseAddress);
        public void Write(byte[] buf, int offset, int size)
        {
            if (size > (this._size - 4))
            {
                throw new ArgumentOutOfRangeException("size is too big!");
            }
            Marshal.WriteInt32(this._hMemory, size);
            IntPtr destination = new IntPtr(this._hMemory.ToInt32() + 4);
            Marshal.Copy(buf, offset, destination, size);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -