1.c++进程之间
#include <stdafx.h>
#include <windows.h>
#include <iostream>
#define BUF_SIZE 4096
using namespace std;
void raedSharedMemCpp()
{
LPVOID pBuffer;
HANDLE hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS,0,L"global_shared_memory_cpp");
if(NULL == hMap)
{
DWORD err= ::GetLastError();
cout << "打开内存失败...." << err << endl;
}
else
{
while(true)
{
pBuffer = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS,0,0,0);
char buf[BUF_SIZE] = {0};
strcpy_s(buf, (char*)pBuffer);
cout << "读取内存:" << buf << endl;
Sleep(100);
}
}
}
void writeSharedMemCpp()
{
char buf[] = "write hello by cpp SharedMem";
HANDLE hMap = CreateFileMapping(INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
BUF_SIZE,
L"global_shared_memory_cpp");
//获取共享内存指针
LPVOID lpBase = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS,0,0,BUF_SIZE);
strcpy((char*)lpBase, buf);
Sleep(20000);
UnmapViewOfFile(lpBase); // 解除文件映射
CloseHandle(hMap);
cout << "write sharedMemCpp end" << endl;
return;
}
2.c#进程之间
#using System;
#using System.Runtime.InteropServices;
#using System.IO.MemoryMappedFiles;
#include System.Collections.Generic;
#using System.Diagnostics;
namespace ColsoleAppCsharp
{
pulbic partial class SharedMemory
{
public static void WriteSharedMemCs()
{
int size = 1024;
MemoryMappedFile shardMemory =
MemoryMappedFile.CreateOrOpen("global_share_memory_Cs",size);
System.Threading.Thread.Sleep(30);
var stream = sharedMemory.CreateViewStream(0,size);
string value = "write by CSharp content";
byte[] data = System.Text.Encoding.UTF8.GEtBytes(value);
stream.Write(data, 0, data.Length);
stream.Dispose();
}
public static void readSharedMemCs()
{
int size = 1024;
MemoryMappedFile mmf =
MemoryMappedFile.OpenExisting("global_share_memory_Cs");
using(var mmfStream =
mmf.CreateViewStream(0,size,MemoryMappedFileAccess.ReadWrite))
{
byte[] buffer = new byte[128];
int nLen = 0;
StringBuilder sb = new StringBuilder();
do
{
nLen = mmfStream.Read(buffer, 0, 128);
sb.AppendLine(System.Text.ASCIIEncoding.GetString(buffer));
}while(nLen>0)
Console.WriteLine("读取内
存:"+sb.ToString().Replace("\0",null).TrimEnd());
}
}
}
}
3.qt进程之间
#include <QSharedMemory>
#include <QDebug>
#include <QByteArray>
using namespace std;
using namespace std::chrono;
void readSharedMemQt()
{
QSharedMemory sharedMemory;
sharedMemory.setKey("global_share_memory_Qt");
QString tmpStr("");
while(true)
{
char c;
// 绑定进程
if(!sharedMemory.isAttached())
{
if(!sharedMemory.attach())
{
qDebug() << sharedMemory.errorString();
}
else
{
sharedMemory.lock();
QByteArray arr((char*)sharedMemory.data(),sharedMemory.size());
tmpStr = arr;
sharedMemory.unlock();
sharedMemory.detach();
std::this_thread::sleep_for(milliseconds(30));
}
}
}
}
void writeSharedMemQt()
{
QSharedMemory sharedMemory;
sharedMemory.setKey("global_share_memory_Qt");
if(sharedMemory.isAttched())
{
qDebug() << "detach" << sharedMemory.detach();
}
QByteArray arr = "write by Qt shared Mem";
int len = arr.length();
if(!sharedMemory.create(len))
{
qDebug() << sharedMemory.errorString();
}
sharedMemory.lock();
char* dest = reinterpret_cast<char*>(sharedMemory.data());
for(int i = 0; i < len; i++)
{
dest[i] = arr[i];
}
sharedMemory.unlock();
}
4.c++进程与c#进程之间,读写共享内存。
参见《1.c++进程之间》和《2.c#进程之间》,一边读,另一边写就行了。
5.qt进程与c#进程之间,不行。
|