当前位置: 代码网 > it编程>编程语言>C# > C#自定义实现多程序共享内存空间

C#自定义实现多程序共享内存空间

2024年11月25日 C# 我要评论
创建一个多个程序共享的内存空间/// <summary> /// 开辟本地自定义的共享内存区域类 /// </summary> public class mc

创建一个多个程序共享的内存空间

/// <summary>
    /// 开辟本地自定义的共享内存区域类
    /// </summary>
    public class mcimsharedmemory<t> : idisposable where t : new()
    {
        /// <summary>
        /// 自定义的内存区域的名
        /// </summary>
        public readonly string mapname = @"local\cimsharedmemory";
        private memorymappedfilesecurity filesecurity = null;
        private memorymappedfile memorymapped = null;
        /// <summary>
        /// 可随机访问的内存块
        /// </summary>
        private memorymappedviewaccessor memoryaccessor = null;
        private int capacity = 0;
 
        public t value { get; set; }
 
        /// <summary>
        ///  开辟本地自定义的共享内存区域类构造方法
        /// </summary>
        public mcimsharedmemory()
        {
            this.filesecurity = new memorymappedfilesecurity();
            this.filesecurity.addaccessrule(new accessrule<memorymappedfilerights>("everyone", memorymappedfilerights.fullcontrol, accesscontroltype.allow));
 
            // memory map 
            this.value = new t();
 
            // 获取结构体大小
            this.capacity = marshal.sizeof(typeof(t));
        }
 
        /// <summary>
        /// 析构函数
        /// </summary>
        ~mcimsharedmemory()
        {
            dispose();
        }
 
        /// <summary>
        /// 创建并打开自定义的本地共享内存区域
        /// </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>
        /// 从文件创建自定义的共享内存区域
        /// </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>
        /// 打开自定义的共享内存区域
        /// </summary>
        public void opensharedmemory()
        {
            this.memorymapped = memorymappedfile.openexisting(this.mapname);
            this.memoryaccessor = this.memorymapped.createviewaccessor();
        }
        /// <summary>
        /// 读取自定义的共享内存区域
        /// </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>
        /// 向随机内存写入值
        /// </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];
        }
    }

应用。

a程序:填充自定义内存空间

namespace 共享内存区域1
{
    class program
    {
        static void main(string[] args)
        {
            console.writeline("共享区域");
            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程序:读取自定义共享内存数据。

namespace 共享内存区域2
{
    class program
    {
        static void main(string[] args)
        {
            console.writeline("共享区域");
            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();
        }
    }
}

到此这篇关于c#自定义实现多程序共享内存空间的文章就介绍到这了,更多相关c#多程序共享内存内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com