一、进程之间数据的共享:共用内存vc++技术
进程之间数据的共享可以用共用内存实现,在Win32中,进程之间共享内存使用的事映射文件。虚拟内存系统具有把实际内存映射到页文件或者交换文件的能力。用户可以把内存映射到任何的映射文件中,包括系统内存页。而利用系统内存页可以实现快捷的内存共享。
利用共有内存实现进程之间的数据共享共有两部:
一. 使用CreateFileMapping函数创建内存映射文件。此函数需要文件句柄,对于大多数的内存共享应用程序,建此文件句柄设置为0xFFFFFFFF即可。这样的句柄指向系统内存页文件。
二. 映射文件文件创建成功以后,以其返回的句柄作为参数,调用MapViewOfFile函数为内存映射文件对象创建视,MapViewOfFile函数将返回指向文件的视指针。可以利用此视指针对内存映射文件进行操作,内存的读写简化到了就像普通变量的操作。
下面是一个利用共有内存实现进程之间的数据共享的Sample程序。
//写内存程序
//MemWrite.cpp
#include "stdafx.h"
#include "windows.h"
#include "iostream.h"
#include "windows.h"
#include "iostream.h"
class student
{
public:
long ID;
char name[20];
};
{
public:
long ID;
char name[20];
};
void main()
{
{
HANDLE hMemShare;
student stu;
int stu_num = 30;
int stu_num = 30;
student *lpstu;
stu.ID = 99041232;
strcpy(stu.name,"SecBug");
strcpy(stu.name,"SecBug");
hMemShare = CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,sizeof(student),"TestMemShare");
if(hMemShare == NULL)
{
cout<<"Failed to Alloocate"<<endl;
return;
}
{
cout<<"Failed to Alloocate"<<endl;
return;
}
lpstu = (student *)MapViewOfFile(hMemShare, FILE_MAP_WRITE,0,0,sizeof(student));
if(lpstu == NULL)
{
cout<<"Failed to Map"<<endl;
return;
}
{
cout<<"Failed to Map"<<endl;
return;
}
*lpstu = stu;
while(1){}
UnmapViewOfFile(lpstu);
}
}
//读内存程序
// MemRead.cpp
#include "stdafx.h"
#include "windows.h"
#include "iostream.h"
#include "iostream.h"
class student
{
public:
long ID;
char name[20];
};
{
public:
long ID;
char name[20];
};
void main()
{
{
HANDLE hMemShare;
student stu;
student *lpstu;
char *lpTch;
stu.ID = 0;
strcpy(stu.name ,"tst");
hMemShare = OpenFileMapping(FILE_MAP_READ,FALSE,"TestMemShare");
if(hMemShare == NULL)
{
{
cout<<"File Created Failed"<<endl;
return;
}
}
lpstu = (student *)MapViewOfFile(hMemShare, FILE_MAP_READ,0,0,sizeof(student));
if(lpTch == NULL)
{
cout<<"Failed to Map"<<endl;
return;
}
{
cout<<"Failed to Map"<<endl;
return;
}
stu = *lpstu;
cout<<stu.ID<<endl;
cout<<stu.name<<endl;
cout<<stu.name<<endl;
UnmapViewOfFile(lpstu);
}
}
二、共享内存实现进程间大数据的交换
三、
Windows进程间通信的各种方法 共介绍了13种,也有一些具体实现的连接,比较不错
四、实时交互应用架构——进程间高效通信方式问题 只是提问式的回答 socket.io开发
五、
进程间通信方式中一般公司用的最多的是哪几个?做嵌入式的 百度提问