当前位置:网站首页>MFC 进程间通信(共享内存)
MFC 进程间通信(共享内存)
2022-08-11 04:57:00 【DuGuYiZhao】
应用:32位SDK与64位进程间通信
目录
1.定义
共享文件句柄、映射缓冲区视图
// FW64To32Dlg.h : 头文件
HANDLE shared_file; //共享文件句柄
LPVOID lpBUF; //映射缓冲区视图
bool bStopRev; //停止接收
// FW64To32Dlg.cpp : 实现文件
#include <iostream>
#include <comdef.h>
#include <Windows.h>
using namespace std;
#define BUF_SIZE 4096
HANDLE H_Mutex = NULL;
HANDLE H_Event = NULL;
HANDLE H_Mutex_REV = NULL;
HANDLE H_Event_REV = NULL;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define EXCHANGE_REV_SEND 1
#if EXCHANGE_REV_SEND
#define REV_E_TEXT L"sm_event_rev"
#define REV_M_TEXT L"sm_mutex_rev"
#define E_TEXT L"sm_event"
#define M_TEXT L"sm_mutex"
#else
#define REV_E_TEXT L"sm_event"
#define REV_M_TEXT L"sm_mutex"
#define E_TEXT L"sm_event_rev"
#define M_TEXT L"sm_mutex_rev"
#endif
1.1接收线程
采用:AfxBeginThread方式
UINT ThreadProc(LPVOID pParam)
{
CFW64To32Dlg* pThis = (CFW64To32Dlg*)pParam;
if(pThis== NULL)
{
return 0;
}
//循环接收
char Buff[97];
while(1&&!(pThis->bStopRev))
{
WaitForSingleObject(H_Event_REV, INFINITE);
WaitForSingleObject(H_Mutex_REV, INFINITE);//使用互斥体加锁
memcpy(Buff, pThis->lpBUF, strlen((char*)(pThis->lpBUF))+1);
ReleaseMutex(H_Mutex_REV);
int nSizeBuff=strlen(Buff);
Buff[nSizeBuff] = '\0';
CString szTest(Buff);
//szTest.Format(_T("%s"),Buff);//乱码
CString cStrReceive;
pThis->m_TxtReceive.GetWindowText(cStrReceive);
cStrReceive +=L"\r\n" + szTest;
pThis->m_TxtReceive.SetWindowText(cStrReceive);
}
AfxEndThread(1);
return 0;
}
2.初始化
创建共享文件句柄
映射缓冲区视图,得到指向共享内存的指针
启动接收线程:AfxBeginThread
BOOL CFW64To32Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// ...
// TODO: 在此添加额外的初始化代码
//S1:创建共享文件句柄
shared_file= CreateFileMapping(INVALID_HANDLE_VALUE,//物理文件句柄CreateFileMapping
NULL,//默认安全级别
PAGE_READWRITE,//可读可写
0,//高位文件大小
BUF_SIZE,//低位文件大小
L"ShareMemory"//共享内存名称
);
if(shared_file == NULL)
{
cout<<"Could not create file mapping object..."<<endl;
return 1;
}
//S2:映射缓冲区视图,得到指向共享内存的指针
lpBUF = MapViewOfFile(
shared_file,//已创建的文件映射对象句柄
FILE_MAP_ALL_ACCESS,//访问模式:可读写
0,//文件偏移的高32位
0,//文件偏移的低32位
BUF_SIZE
);
if(lpBUF==NULL)
{
cout<<"Could not create file mapping object..."<<endl;
CloseHandle(shared_file);
return 1;
}
//发送
H_Mutex = CreateMutex(NULL, FALSE, M_TEXT);
H_Event = CreateEvent(NULL, FALSE, FALSE, E_TEXT);
//接收
H_Mutex_REV = CreateMutex(NULL, FALSE, REV_M_TEXT);
H_Event_REV = CreateEvent(NULL, FALSE, FALSE, REV_E_TEXT);
bStopRev=false;
CWinThread* pThread = AfxBeginThread(ThreadProc, this);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
3.发送
操作共享内存:填充数据;
void CFW64To32Dlg::OnBnClickedSend()
{
// TODO: 在此添加控件通知处理程序代码
CString strSend;
m_TxtSend.GetWindowText(strSend);
if(strSend=="" )
{
::AfxMessageBox(L"发送内容不能为空!");
m_TxtSend.SetFocus();
return;
}
//S3:操作共享内存
wchar_t* wBuff=(LPTSTR)(LPCTSTR)strSend;
_bstr_t b(wBuff);
const char* Buff = b;
WaitForSingleObject(H_Mutex, INFINITE);//使用互斥体加锁
memcpy(lpBUF, Buff, strlen(Buff)+1);
ReleaseMutex(H_Mutex);
SetEvent(H_Event);
}
4.退出
停止接收
解除映射和关闭句柄
void CFW64To32Dlg::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
bStopRev=true;
SetEvent(H_Event_REV);
Sleep(1000);
CloseHandle(H_Mutex);
CloseHandle(H_Event);
CloseHandle(H_Mutex_REV);
CloseHandle(H_Event_REV);
//S4:解除映射和关闭句柄
UnmapViewOfFile(lpBUF);
CloseHandle(shared_file);
CDialogEx::OnClose();
}
边栏推荐
- Overview of the JVM garbage collection and mechanism
- 如何阅读论文
- How IP-Guard prohibits running U disk programs
- 一起Talk编程语言吧
- jwsManager服务接口实现类-jni实现
- 交换机和路由器技术-31-扩展ACL
- ALSA音频架构 -- aplay播放流程分析
- 绿盾加密如何顺利切换成IP-Guard加密
- 论文笔记:Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
- 网络安全培训机构哪家好?排名怎么选择?
猜你喜欢
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/data/xxxx
Switch and Router Technology-29-OSPF Virtual Link
分层架构&SOA架构
优化是一种习惯●出发点是“站在靠近临界“的地方
The sword refers to offer_abstract modeling capabilities
Application layer protocol - DNS
Switch and Router Technology - 36-Port Mirroring
交换机--- 生成树--三层架构总结
Embedded Sharing Collection 33
0 Basic software test for career change, self-study for 3 months, 12k*13 salary offer
随机推荐
K8s Review Notes 7--K8S Implementation of Redis Standalone and Redis-cluster
zabbix构建企业级监控告警平台
Bubble sort and heap sort
Paper Notes: Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
Snap - rotate the smallest number of an array
The sword refers to offer_abstract modeling capabilities
网络协议1
如何阅读论文
The principle, architecture, implementation, practice of "transfer" and "search", no need to be afraid of interviews
源代码加密技术浅析
About data paging display
【FPGA教程案例49】控制案例1——基于FPGA的PID控制器verilog实现
分层架构&SOA架构
Redis deletes keys in batches according to regular rules
【Web3 系列开发教程——创建你的第一个 NFT(9)】如何在手机钱包里查看你的 NFT
findViewById返回null的问题
ALSA音频架构
C语言题解:谁是凶手!
[Server installation mysql] Use mysql offline installation package to install mysql5.7 under centos7
Switches and routers technologies - 30 - standard acls