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

相关推荐
没什么本事11 小时前
关于C# panel 添加lable问题 -- 明确X和Y 位置错误
android·java·c#
火星papa13 小时前
C# 实现平滑流畅的进度条ProgressBar
c#·进度条·progressbar·平滑流畅
游乐码15 小时前
UnityGUI(五)GUI控件综合使用
开发语言·unity·c#
程序leo源15 小时前
C语言知识总结
c语言·开发语言·c++·经验分享·笔记·青少年编程·c#
烛阴16 小时前
TEngine 入门系列(二):三件套环境搭建 -- Unity + TEngine + AI 助手
前端·c#·unity3d
The Shio20 小时前
OptiByte 操练场:面向 IoT/嵌入式的协议可视化调试工具
网络·嵌入式硬件·物联网·c#·.net·业界资讯·iot
龙侠九重天21 小时前
C# 调用 TensorFlow:迁移学习与模型推理实战指南
人工智能·深度学习·机器学习·c#·tensorflow·迁移学习·tensorflow.net
我是唐青枫1 天前
C#.NET YARP 认证授权实战:在网关层统一接入 JWT
开发语言·c#·.net
程序leo源1 天前
Linux深度理解
linux·运维·服务器·c语言·c++·青少年编程·c#
白菜上路1 天前
C# .net 生成版本号自动变更
c#·.net·visual studio