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

相关推荐
kaikaile19958 小时前
数字全息图处理系统(C# 实现)
开发语言·c#
wearegogog12313 小时前
C# .NET 文件比较工具 WinForms
开发语言·c#·.net
糖不吃13 小时前
WPF值转换器
c#
Popeye-lxw15 小时前
由罗技 K380 键盘 FN 键模式切换引发的血案
c#
FL162386312915 小时前
C# OpenCvSharp 基于霍夫变换直线检测的文本图像倾斜校正文本图像倾斜校
开发语言·c#
aini_lovee17 小时前
C# 快递单打印系统(万能套打系统)
开发语言·c#
白菜上路17 小时前
C# Serilog.AspNetCore基本使用
c#·serilog
小白不白11117 小时前
C# WinForm 与 VP 二次开发
开发语言·c#
SunnyDays101118 小时前
如何使用 C# 自动调整 Excel 行高和列宽
开发语言·c#·excel
itgather19 小时前
OfficeExcel — Word / Excel DLL 验证台功能介绍
c#·word·excel