最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#自定義實(shí)現(xiàn)多程序共享內(nèi)存空間

 更新時(shí)間:2024年10月28日 09:18:48   作者:lingxiao16888  
這篇文章主要為大家詳細(xì)介紹了C#如何自定義實(shí)現(xiàn)多程序共享內(nèi)存空間,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

創(chuàng)建一個(gè)多個(gè)程序共享的內(nèi)存空間

/// <summary>
    /// 開(kāi)辟本地自定義的共享內(nèi)存區(qū)域類
    /// </summary>
    public class MCimSharedMemory<T> : IDisposable where T : new()
    {
        /// <summary>
        /// 自定義的內(nèi)存區(qū)域的名
        /// </summary>
        public readonly string MapName = @"Local\CimSharedMemory";
        private MemoryMappedFileSecurity FileSecurity = null;
        private MemoryMappedFile MemoryMapped = null;
        /// <summary>
        /// 可隨機(jī)訪問(wèn)的內(nèi)存塊
        /// </summary>
        private MemoryMappedViewAccessor MemoryAccessor = null;
        private int Capacity = 0;
 
        public T Value { get; set; }
 
        /// <summary>
        ///  開(kāi)辟本地自定義的共享內(nèi)存區(qū)域類構(gòu)造方法
        /// </summary>
        public MCimSharedMemory()
        {
            this.FileSecurity = new MemoryMappedFileSecurity();
            this.FileSecurity.AddAccessRule(new AccessRule<MemoryMappedFileRights>("everyone", MemoryMappedFileRights.FullControl, AccessControlType.Allow));
 
            // Memory Map 
            this.Value = new T();
 
            // 獲取結(jié)構(gòu)體大小
            this.Capacity = Marshal.SizeOf(typeof(T));
        }
 
        /// <summary>
        /// 析構(gòu)函數(shù)
        /// </summary>
        ~MCimSharedMemory()
        {
            Dispose();
        }
 
        /// <summary>
        /// 創(chuàng)建并打開(kāi)自定義的本地共享內(nèi)存區(qū)域
        /// </summary>
        public void CreateOrOpenSharedMemory()
        {
            this.MemoryMapped = MemoryMappedFile.CreateOrOpen(this.MapName,
                                            this.Capacity,
                                            MemoryMappedFileAccess.ReadWriteExecute,
                                            MemoryMappedFileOptions.None,
                                            this.FileSecurity,
                                            HandleInheritability.Inheritable);
 
            this.MemoryAccessor = this.MemoryMapped.CreateViewAccessor();
        }
        /// <summary>
        /// 從文件創(chuàng)建自定義的共享內(nèi)存區(qū)域
        /// </summary>
        public void CreateFromFileSharedMemory()
        {
            this.MemoryMapped = MemoryMappedFile.CreateFromFile(new FileStream(@"", FileMode.Create),
                                            this.MapName,
                                            this.Capacity,
                                            MemoryMappedFileAccess.ReadWriteExecute,
                                            this.FileSecurity,
                                            HandleInheritability.Inheritable,
                                            true);
 
            this.MemoryAccessor = this.MemoryMapped.CreateViewAccessor();
        }
 
       
        public void CreateSharedMemory()
        {
            this.MemoryMapped = MemoryMappedFile.CreateNew(this.MapName,
                                            this.Capacity,
                                            MemoryMappedFileAccess.ReadWriteExecute,
                                            MemoryMappedFileOptions.None,
                                            this.FileSecurity,
                                            HandleInheritability.Inheritable);
 
            this.MemoryAccessor = this.MemoryMapped.CreateViewAccessor();
        }
 
        /// <summary>
        /// 打開(kāi)自定義的共享內(nèi)存區(qū)域
        /// </summary>
        public void OpenSharedMemory()
        {
            this.MemoryMapped = MemoryMappedFile.OpenExisting(this.MapName);
            this.MemoryAccessor = this.MemoryMapped.CreateViewAccessor();
        }
        /// <summary>
        /// 讀取自定義的共享內(nèi)存區(qū)域
        /// </summary>
        /// <returns></returns>
        public T ReadMemory()
        {
            byte[] bytes = new byte[this.Capacity];
 
            // Initialize unmanaged memory.
            IntPtr p = Marshal.AllocHGlobal(this.Capacity);
 
            // Read from memory mapped file.
            this.MemoryAccessor.ReadArray<byte>(0, bytes, 0, bytes.Length);
 
            // Copy from byte array to unmanaged memory.
            Marshal.Copy(bytes, 0, p, this.Capacity);
 
            // Copy unmanaged memory to struct.
            T ReadData = (T)Marshal.PtrToStructure(p, typeof(T));
 
            // Free unmanaged memory.
            Marshal.FreeHGlobal(p);
            p = IntPtr.Zero;
 
            // Value assign
            this.Value = ReadData;
 
            return ReadData;
        }
        /// <summary>
        /// 向隨機(jī)內(nèi)存寫入值
        /// </summary>
        public void WriteMemory()
        {
            byte[] bytes = new byte[this.Capacity];
 
            // Initialize unmanaged memory.
            IntPtr p = Marshal.AllocHGlobal(this.Capacity);
 
            // Copy struct to unmanaged memory.
            Marshal.StructureToPtr(this.Value, p, false);
 
            // Copy from unmanaged memory to byte array.
            Marshal.Copy(p, bytes, 0, this.Capacity);
 
            this.MemoryAccessor.WriteArray<byte>(0, bytes, 0, bytes.Length);
 
            // Free unmanaged memory.
            Marshal.FreeHGlobal(p);
            p = IntPtr.Zero;
        }
 
        public void Dispose()
        {
            if (this.MemoryAccessor != null)
            {
                this.MemoryAccessor.Dispose();
                this.MemoryAccessor = null;
            }
 
            if (MemoryMapped != null)
            {
                this.MemoryMapped.Dispose();
                this.MemoryMapped = null;
            }
        }
    }
  
 
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
    public class SCimMemory
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0xFFFF)] public short[] Bit;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x3FFFF)] public short[] Word;
 
        public SCimMemory()
        {
            this.Bit = new short[0xFFFF];
            this.Word = new short[0x3FFFF];
        }
    }

應(yīng)用。

A程序:填充自定義內(nèi)存空間

namespace 共享內(nèi)存區(qū)域1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("共享區(qū)域");
            MCimSharedMemory<SCimMemory> memory = new MCimSharedMemory<SCimMemory>();
            memory.CreateOrOpenSharedMemory();
            Console.WriteLine("寫入值");
            memory.Value = new SCimMemory();
            memory.Value.Bit[0] = 100;
            memory.Value.Word[0] = 120;
            memory.WriteMemory();
            
            Console.WriteLine("輸入完成");
            Console.ReadKey();
        }
    }
}

B程序:讀取自定義共享內(nèi)存數(shù)據(jù)。

namespace 共享內(nèi)存區(qū)域2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("共享區(qū)域");
            MCimSharedMemory<SCimMemory> memory = new MCimSharedMemory<SCimMemory>();
            memory.CreateOrOpenSharedMemory();
            Console.WriteLine("讀取值");
            memory.ReadMemory();
            Console.WriteLine($"B0={memory.Value.Bit[0]},W0={memory.Value.Word[0]}");
 
            Console.WriteLine("輸入完成");
            Console.ReadKey();
        }
    }
}

到此這篇關(guān)于C#自定義實(shí)現(xiàn)多程序共享內(nèi)存空間的文章就介紹到這了,更多相關(guān)C#多程序共享內(nèi)存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

北安市| 博客| 牙克石市| 剑川县| 永春县| 元朗区| 特克斯县| 聂拉木县| 桃源县| 香港| 宁武县| 太仆寺旗| 台安县| 樟树市| 行唐县| 吉水县| 南宁市| 新巴尔虎左旗| 辽宁省| 三门县| 微山县| 正定县| 林西县| 汪清县| 犍为县| 和林格尔县| 揭西县| 万山特区| 三亚市| 潮安县| 固始县| 皋兰县| 塘沽区| 临潭县| 宜宾县| 伊通| 大渡口区| 渝中区| 凤阳县| 河北省| 资中县|