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

相关推荐
yue0089 小时前
winform控件篇-按钮
c#
DevOpenClub13 小时前
Markdown、HTML 和 PPT 如何稳定交付:文档转换任务的幂等发布流程
开发语言·前端·c#·html·powerpoint
软件黑马王子14 小时前
3.单例模式问题1:构造函数
开发语言·单例模式·c#
软件黑马王子14 小时前
4.单例模式问题2:重复挂载
开发语言·单例模式·c#
夏霞16 小时前
c# 不支持的目标框架 解决方案
开发语言·c#
软件黑马王子16 小时前
5.单例模式问题3:多线程问题
单例模式·前端框架·c#
geovindu16 小时前
CSharp: Template Method Pattern
开发语言·后端·c#·.net·模板方法模式·行为模式
CSDN_RTKLIB16 小时前
SetImpliedSelection
c#
软件黑马王子16 小时前
1.non-MonoBehaviour 单例模式泛型基类
unity·前端框架·c#
高彬17 小时前
SAP ECC6.0 对接 C# web Services服务
开发语言·前端·c#·sap