C#自定義實(shí)現(xiàn)多程序共享內(nèi)存空間
更新時間:2024年10月28日 09:18:48 作者:lingxiao16888
這篇文章主要為大家詳細(xì)介紹了C#如何自定義實(shí)現(xiàn)多程序共享內(nèi)存空間,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
創(chuàng)建一個多個程序共享的內(nèi)存空間
/// <summary> /// 開辟本地自定義的共享內(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ī)訪問的內(nèi)存塊 /// </summary> private MemoryMappedViewAccessor MemoryAccessor = null; private int Capacity = 0; public T Value { get; set; } /// <summary> /// 開辟本地自定義的共享內(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)建并打開自定義的本地共享內(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> /// 打開自定義的共享內(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 實(shí)現(xiàn)把double 存成兩位精度小數(shù)
這篇文章主要介紹了C# 實(shí)現(xiàn)把double 存成兩位精度小數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12WinForm開發(fā)中屏蔽WebBrowser腳本錯誤提示的方法
這篇文章主要介紹了WinForm開發(fā)中屏蔽WebBrowser腳本錯誤提示的方法,在C#項(xiàng)目開發(fā)中比較實(shí)用,需要的朋友可以參考下2014-08-08C#使用ILGenerator動態(tài)生成函數(shù)的簡單代碼
這篇文章主要介紹了C#使用ILGenerator動態(tài)生成函數(shù)的簡單代碼,需要的朋友可以參考下2017-08-08