VS-C++ 系列:所有相关C++文章链接.
VS-C# 系列:所有相关C#文章链接.
bat 系列:所有相关bat文章链接.
OpenCV 系列:所有相关OpenCV文章链接.
Keil 系列:所有相关文章链接
所有内容均以最小系统调试成功;逐步提供低分源码工程下载!
保证每行代码都经过验证!
如有疑惑,欢迎留言,看见即回;祝好__by Dxg_LC
序言:
1、以上链接为方便整理查看资料用;伴随博文发布更新,如果有不正确处,感谢指正
2、因本人能力有限若有不正确之处或者相关超链接失效,请于相关文章内提醒@博主;灰常感谢
3、友情提醒1,勿要《一支烟 + 一杯茶 == 一坐一下午》 身体重要,革命本钱;
4、友情提醒2,多喝热水;
5、友情提醒3,听媳妇话+多点时间陪家人;
using System;
using System.Windows.Forms;
//
using System.IO.MemoryMappedFiles;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
MemoryMappedFile shareMemory = null;
#region Sts
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct ShareMem_Head
{
///
/// 身份;1-C#写入,2-C++写入
///
public int ID;
///
/// 函数序号;定义参考.h的序号
///
public int FunCode;
///
/// 返回结果;0-初始值;1-完成;!1-错误代码
///
public int Result;
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct ShareMem_Rect
{
public int x;
public int y;
public int w;
public int h;
public ShareMem_Rect(int _x, int _y, int _w, int _h)
{
this.x = _x;
this.y = _y;
this.w = _w;
this.h = _h;
}
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct ShareMem_St1
{
///
/// 图片绝对地址
///
public int pImageLen;
///
/// 矩形结构体
///
public ShareMem_Rect ImgRect;
///
///
///
public int kind;
///
///
///
public int pos;
}
#endregion
//创建并初始化 共享内存
private unsafe void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "释放共享内存")
{
shareMemory.Dispose();
Console.WriteLine("...释放共享内存完成\r\n");
button1.Text = "创建共享内存";
}
else
{
//创建共享内存
shareMemory = MemoryMappedFile.CreateOrOpen(ShareMem, 1024*10, MemoryMappedFileAccess.ReadWrite);
Console.WriteLine("创建共享内存完成...\r\n");
button1.Text = "释放共享内存";
}
}
//写 string 到 Mem
private unsafe void ShareMem_WriteString(MemoryMappedViewAccessor pMMVA, byte[] pbyte, int Offset)
{
for (int i = 0; i < pstr.Length; i++)
{
pMMVA.Write(Offset + i, pbyte[i]);
}
}
//读 Mem 到 string
private unsafe string ShareMem_ReadString(MemoryMappedViewAccessor pMMVA, int StrLen, int Offset)
{
byte[] pbyte = new byte[StrLen];
for (int i = 0; i < StrLen; i++)
{
pbyte[i] = pMMVA.ReadByte(Offset + i);
}
return System.Text.Encoding.Default.GetString(pbyte);
}
//写共享内存
private unsafe void button2_Click(object sender, EventArgs e)
{
var viewAccessor = shareMemory.CreateViewAccessor(0, 1024*10);
ShareMem_Head pHead = new ShareMem_Head();
pHead.ID = 1;
pHead.FunCode = 1;
pHead.Result = 0;
int offset = 0;
string ImgPath = "";
byte[] pbyte;
switch (pHead.FunCode)
{
case 0:
viewAccessor.Write<ShareMem_Head>(offset, ref pHead);
offset += sizeof(ShareMem_Head);
break;
case 1:
viewAccessor.Write<ShareMem_Head>(offset, ref pHead);
offset += sizeof(ShareMem_Head);
ImgPath = "D:\\test.jpg";
pbyte = System.Text.Encoding.Default.GetBytes(ImgPath);
ShareMem_St1 pSt1 = new ShareMem_St1();
pSt2.kind = 2;
pSt2.pos = 1;
pSt2.ImgRect = new ShareMem_Rect(11, 12, 13, 14);
pSt2.pImageLen = pbyte .Length;
viewAccessor.Write<ShareMem_St1>(offset, ref pSt1);
offset += sizeof(EIP_St2ShareMem_St1);
ShareMem_WriteString(viewAccessor, pbyte, offset);
offset += pbyte.Length;
break;
default :
pHead.ID = -1;
pHead.FunCode = -1;
viewAccessor.Write<ShareMem_Head>(0, ref pHead);
offset += sizeof(ShareMem_Head);
break;
}
}
//读共享内存
private unsafe void button3_Click(object sender, EventArgs e)
{
var viewAccessor = shareMemory.CreateViewAccessor(0, 1024*10);
int offset = 0;
string OutStr = "";
ShareMem_Head pHead = new ShareMem_Head();
viewAccessor.Read<ShareMem_Head>(offset,out pHead);
offset += sizeof(ShareMem_Head);
if (pHead.ID == 2)
{
switch (pHead.FunCode)
{
case 0:
OutStr += "收到《0》的" + ((pHead.Result==1)?"成功信息":"失败信息,错误代码:"+ pHead.Result.ToString());
break;
case 1:
OutStr += "收到《1》的" + ((pHead.Result==1)?"成功信息":"失败信息,错误代码:"+ pHead.Result.ToString());
ShareMem_St1 pSt1 = new ShareMem_St1();
viewAccessor.Read<ShareMem_St1>(offset, out pSt1);
offset += sizeof(ShareMem_St1);
break;
default:
OutStr += "未知函数代码";
break;
}
}
else if (pHead.ID == 1)
{
OutStr += "C++ 还未返回/未接受到ShareMem";
}
else { OutStr += "未知身份。"; }
Console.WriteLine(DateTime.Now.ToString() + ": " + OutStr+"\r\n");
}
//控制台程序
#include
#include
using namespace std;
struct ShareMem_Head
{
///
/// 身份;1-C#写入,2-C++写入
///
int ID;
///
/// 函数序号;定义参考.h的序号
///
int FunCode;
///
/// 返回结果;0-初始值;1-完成;!1-错误代码
///
int Result;
};
struct ShareMem_Rect
{
int x;
int y;
int w;
int h;
};
struct ShareMem_St1
{
///
/// 图片绝对地址
///
int pImageLen;
///
/// 矩形结构体
///
ShareMem_Rect ImgRect;
///
///
///
int kind;
///
///
///
int pos;
};
LPCWSTR stringToLPCWSTR(std::string orig)
{
size_t origsize = orig.length() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(orig.length() - 1));
mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
return wcstring;
}
int main()
{
LPVOID pBuffer;
string strMapName("share_memory");
HANDLE hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, stringToLPCWSTR(strMapName));
ShareMem_Head pHead;
ShareMem_St1 PSt1;
if (NULL == hMap) cout << "无共享内存..." << endl;
else
{
int Offset = 0;
char ppp[255];
while (true)
{
Sleep(1);
pBuffer = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
Offset = 0;
memcpy(&pHead, pBuffer, sizeof(ShareMem_Head));
Offset += sizeof(ShareMem_Head);
if(pHead.ID == 1)
switch (pHead.FunCode)
{
case 0:
break;
case 1:
memcpy(&PSt1, (char*)pBuffer + Offset, sizeof(ShareMem_St1));
Offset += sizeof(ShareMem_St1 );
memset(ppp, 0, 255);
memcpy(&ppp, (char*)pBuffer + Offset, PSt1.pImageLen);
printf(ppp);printf("\r\n");
Offset += PSt1.pImageLen;
break;
}
}
}
getchar();
CloseHandle(hMap);
}
欢迎收藏,点赞;"一键三联"走起,LOL