当前位置:网站首页>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"
#endif1.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();
}边栏推荐
- JwsManager service interface implementation class - the jni implementation
- form form submission database Chinese becomes a question mark
- 交换机和路由器技术-25-OSPF多区域配置
- Redis deletes keys in batches according to regular rules
- svg-icon的使用方法(svg-sprite-loader插件)
- 绿盾加密如何顺利切换成IP-Guard加密
- C语言题解:谁是凶手!
- paddlepaddle implements CS_CE Loss and incorporates PaddleClas
- 视觉任务种常用的类别文件之一json文件
- 交换机和路由器技术-22/23-OSPF动态路由协议/链路状态同步过程
猜你喜欢

如何给网页添加icon图标?

Switches and routers technology - 21 - RIP routing protocol

To break the bottleneck of transactional work, the gentleman signs the electronic contract to release the "source power" of HR!

Overview of the JVM garbage collection and mechanism

网络技能树

Selenium自动化测试框架工作原理你明白了吗?

Switch and Router Technology-27-OSPF Route Redistribution

2022新员工公司级安全教育基础培训(118页)
![ERROR: Could not install packages due to an OSError: [Errno 2] 没有那个文件或目录: ‘/data/xxxx](/img/02/3896b29a955ae84a0f0326f0d2cabf.png)
ERROR: Could not install packages due to an OSError: [Errno 2] 没有那个文件或目录: ‘/data/xxxx

How to switch Green Shield encryption to IP-Guard encryption smoothly
随机推荐
How to read a paper
交换机和路由器技术-29-OSPF虚链路
Research on a Consensus Mechanism-Based Anti-Runaway Scheme for Digital Trunking Terminals
力扣——青蛙跳台阶问题
延长经济保险(jeecgboot)
Mysql: set the primary key to automatically increase the starting value
Switches and routers technology - 21 - RIP routing protocol
-填涂颜色-
Switches and routers technology - 26 - configure OSPF peripheral area
[FPGA tutorial case 49] Control case 1 - FPGA-based PID controller verilog implementation
标识密码技术在 IMS 网络中的应用
C语言:实用调试技巧
The principle, architecture, implementation, practice of "transfer" and "search", no need to be afraid of interviews
send_sig: kernel execution flow
jwsManager服务接口实现类-jni实现
[Actual combat scene] Mall-discount event design plan
The shortest path out of the maze
Switch and Router Technology - 22/23 - OSPF Dynamic Routing Protocol/Link State Synchronization Process
Redis deletes keys in batches according to regular rules
交换机和路由器技术-25-OSPF多区域配置