我这里做一个简单的示例
1.C#写入内存的方法,FileName是内存共享的名字 t是内存size
public static void SaveGluePLYToMemory(string FileName, string msg)
{
try
{
long t = 100;// SetMemorySize(msg);// 100;
//# 创建内存块,test1,其他语言利用这个内存块名字就能找到内存块。
var mmf = MemoryMappedFile.CreateOrOpen(FileName, t, MemoryMappedFileAccess.ReadWrite);
var viewAccessor = mmf.CreateViewAccessor(0, t);
string s = msg;
viewAccessor.WriteArray<byte>(0, System.Text.Encoding.Default.GetBytes(s), 0, s.Length);
// MessageBox.Show("write ok");
//GC.Collect();
}
catch (System.Exception s)
{
MessageBox.Show(s.Message);
}
}
2.Python读取内存 filename分别为TireGlueing和FoamStick
import copy
import os
import sys
import open3d as o3d;
import numpy as np
import pip;
import mmap
from multiprocessing import shared_memory
if __name__ == "__main__":
print("version")
print(sys.path)
print(o3d.__version__)
print(sys.version)
print(sys.executable)
print(pip.__version__)
print(pip.__file__)
print(sys.getwindowsversion())
try:
MemorySize = 100
file_name1 = 'TireGlueing'
#print(SHMEMSIZE)
# python读取共享内存
shmem1 = mmap.mmap(0, MemorySize, file_name1, mmap.ACCESS_READ)
print(f"TireGlue = {shmem1.read(MemorySize).decode('ASCII')}")
#MemorySize = int(shmem.read(MemorySize).decode('ASCII'))
shmem1.close()
file_name = 'FoamStick'
print(MemorySize)
# python读取共享内存
shmem = mmap.mmap(0, MemorySize, file_name, mmap.ACCESS_READ)
#print(shmem.read(MemorySize).decode('ASCII'))
print(f"FoamStick = {shmem.read(MemorySize).decode('ASCII')}")
shmem.close()
except Exception as e:
print(e)
print('ok')
print("version end")