• C#不通过byte[],直接对内存映射文件复制内存


    背景


    多个进程直接需要传递大量图片,所以对性能要求较高。支付复制内存显然比转成byte[]再复制优越。


    命名空间


    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    代码


     public CMainTestForm()
            {
                InitializeComponent();
                WriteIntToMemFile(34);
            }

            static unsafe void WriteIntToMemFile(int i)
            {
                using (var mmf = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateNew("HZD20230925", 42))
                using (var view = mmf.CreateViewAccessor())
                {
                    byte* poke = null;
                    view.SafeMemoryMappedViewHandle.AcquirePointer(ref poke);
                    CopyMemory(new System.IntPtr(poke), new System.IntPtr(&i), sizeof(int));
                    view.SafeMemoryMappedViewHandle.AcquirePointer(ref poke);
                    int iRead = view.ReadInt32(0);
                    Debug.Assert(iRead == i);
                }
            }

            [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", CharSet = CharSet.Ansi)]
            public extern static long CopyMemory(IntPtr dest, IntPtr source, int size);

    测试环境


    Win7 VS2022

    https://img-blog.csdnimg.cn/ea2601b3918f4aef836b5fe30da2ebf7.gif#pic_center#pic_center

    其它

    学院课程

    基础算法的C++实现课程,请点击下面的CSDN学院的链接。讲义有算法详解。

    2024年1月15之前完全免费,之后绝大部分免费

    https://edu.csdn.net/course/detail/38771

    C#入职培训

    此课程的目的:让新同事更快完成从学生到C#程序员的转换,更快上手完成C#的开发工作。

    https://edu.csdn.net/course/detail/38768

    C++入职培训

    让新同事更快完成从学生到C++程序员的转换,更快上手完成C++的开发工作。

    https://edu.csdn.net/course/detail/32049

    运行验证环境

    Win10 VS2022 Ck++17 或win7 VS2019 C++17

    每天都补充正能量

    好好学习,天天向上。

    事无终始,无务多业。

    是故置本不安者,无务丰末。

    相关下载

    如果你时间宝贵,只想看精华,请到CSDN下载频道下载《闻缺陷则喜算法册》doc版

    https://download.csdn.net/download/he_zhidan/88348653

    https://img-blog.csdnimg.cn/f95ddae62a4e43a68295601c723f92fb.gif#pic_center

  • 相关阅读:
    MySQL数值函数
    pyppeteer框架基本语法备忘录
    CMAKE学习
    冒泡排序Java代码实现
    LeetCode 704. 二分查找
    大数据Doris(二十三):取消导入与其他导入案例参考
    思维模型 协议
    使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏 🌐
    c# 定时器
    小狐狸付费创作系统如何接入Midiourney接口并使用KEY教程
  • 原文地址:https://blog.csdn.net/he_zhidan/article/details/132915336