一、创建对话框应用程序
二、编辑对话框资源
控件ID及标题
IDC_STATIC 发送数据
IDC_EDIT_SEND
IDC_BUTTON_SEND 发送
IDC_EDIT_SEND
IDC_BUTTON_SEND 发送
IDC_STATIC 接收数据
IDC_EDIT_RECEIVE
IDC_BUTTON_RECEIVE 接收
IDOK 退出
IDC_EDIT_RECEIVE
IDC_BUTTON_RECEIVE 接收
IDOK 退出
三、添加变量、函数、对象/库模块
1、添加变量
public:
CEdit m_send;
CEdit m_send;
CEdit m_receive;
public:
HANDLE SharedMemory;
void* address;
HANDLE SharedMemory;
void* address;
2、添加消息响应函数
四、添加代码
1、添加初始化代码
BOOL CThreadDlg::OnInitDialog()
{
CDialog::OnInitDialog();
......
SharedMemory = CreateFileMapping((HANDLE)0xffffffff,NULL,PAGE_READWRITE,0,10000,"MemFile");
address = MapViewOfFile(SharedMemory,FILE_MAP_WRITE,0,0,0);
return TRUE;
}
{
CDialog::OnInitDialog();
......
SharedMemory = CreateFileMapping((HANDLE)0xffffffff,NULL,PAGE_READWRITE,0,10000,"MemFile");
address = MapViewOfFile(SharedMemory,FILE_MAP_WRITE,0,0,0);
return TRUE;
}
2、添加函数代码
void CThreadDlg::OnButtonReceive()
{
// TODO: Add your control notification handler code here
CString str;
{
// TODO: Add your control notification handler code here
CString str;
strcpy((char*)(LPCTSTR)str,(char*)address);
m_receive.SetWindowText(str);
}
m_receive.SetWindowText(str);
}
void CThreadDlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
CString str;
m_send.GetWindowText(str);
strcpy((char*)address,(char*)(LPCTSTR)str);
}
{
// TODO: Add your control notification handler code here
CString str;
m_send.GetWindowText(str);
strcpy((char*)address,(char*)(LPCTSTR)str);
}
五、编译
六、运行
七、函数说明
1、CreateFileMapping()函数声明
HANDLE CreateFileMapping(HANDLE hFile,LPSECURITY_ATTRIBUTE lpFileMappingAttributes,DWORD flProtect,DWORD dwMaximumSizeHigh,DWORD dwMaximumSizeLow,LPCTSTR lpName)
功能:创建一个内存映射对象。成功,返回文件映射对象句柄;失败,返回NULL。
进程间通信 共享内存 2、MapViewOfFile()函数声明
HANDLE MapViewOfFile(HANDLE hFileMappingObject,DWORD dwDesiredAccess,DWORD dwFileOffsetHigh,DWORD dwFileOffsetLow,SIZE_T dwNumberOfByteToMap)
功能:将内存映射对象映射到进程地址控件。成功,返回映射对象的起始地址;失败,返回NULL。
3、strcpy()函数声明
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论