进程一, 建立映射文件,填写数据,并发出Event的信号;
进程二,打开映射文件,收到Event的信号时读取数据.
#include <windows.h>
#include <string.h>
#include <iostream>
using namespace std;
#define FILE_SIZE 1024
static HANDLE hMapFile;
static LPVOID lpMapAddress;
static LPCTSTR g_szEventName="EventName";
void Write(char* data){
if(lpMapAddress!=NULL){
memset(lpMapAddress,0,FILE_SIZE);
sprintf((char*)lpMapAddress,"%s",data);
}
}
void Read(){
if(lpMapAddress!=NULL){
printf("%s\n",lpMapAddress);
}
}
void Process1(){//写数据
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // Current file handle.
NULL, // Default security.
PAGE_READWRITE, // Read/write permission.
0, // Max. object size.
FILE_SIZE, // Size of hFile.
"MyFileMappingObject"); // Name of mapping object.
if (hMapFile == NULL)
{
printf("Could not create file-mapping object.");
return ;
}
//
lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.
FILE_MAP_ALL_ACCESS, // Read/write permission
0, // Max. object size.
0, // Size of hFile.
FILE_SIZE); // Map entire file.
if (lpMapAddress == NULL)
{
printf("Could not map view of file.");
return ;
}
::Sleep(10000);
//Write data
HANDLE hEvent=::OpenEvent(
EVENT_MODIFY_STATE,
FALSE,
g_szEventName);
while(hEvent!=NULL){
scanf("%s",lpMapAddress);
::SetEvent(hEvent);
}
::CloseHandle(hEvent);
hEvent=INVALID_HANDLE_VALUE;
}
void Process2(){//读数据
hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, // Read/write permission.
FALSE, // Do not inherit the name
"MyFileMappingObject"); // of the mapping object.
if (hMapFile == NULL)
{
printf("Could not open file-mapping object.\n");
return;
}
lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.
FILE_MAP_ALL_ACCESS, // Read/write permission.
0, // Max. object size.
0, // Size of hFile.
FILE_SIZE); // Map entire file.
if (lpMapAddress == NULL)
{
printf("Could not map view of file.\n");
return;
}
//Wait for data
HANDLE hEvent=::CreateEvent(
NULL,
TRUE,
FALSE,
g_szEventName);
while(hEvent!=NULL){
::WaitForSingleObject(hEvent,INFINITE);
::ResetEvent(hEvent);
::Read();
}
::CloseHandle(hEvent);
hEvent=INVALID_HANDLE_VALUE;
}
void main(){
int flag;
cin>>flag;
if(flag==1){
Process1();
}
else if(flag==2){
Process2();
}
}
#include <string.h>
#include <iostream>
using namespace std;
#define FILE_SIZE 1024
static HANDLE hMapFile;
static LPVOID lpMapAddress;
static LPCTSTR g_szEventName="EventName";
void Write(char* data){
if(lpMapAddress!=NULL){
memset(lpMapAddress,0,FILE_SIZE);
sprintf((char*)lpMapAddress,"%s",data);
}
}
void Read(){
if(lpMapAddress!=NULL){
printf("%s\n",lpMapAddress);
}
}
void Process1(){//写数据
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // Current file handle.
NULL, // Default security.
PAGE_READWRITE, // Read/write permission.
0, // Max. object size.
FILE_SIZE, // Size of hFile.
"MyFileMappingObject"); // Name of mapping object.
if (hMapFile == NULL)
{
printf("Could not create file-mapping object.");
return ;
}
//
lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.
FILE_MAP_ALL_ACCESS, // Read/write permission
0, // Max. object size.
0, // Size of hFile.
FILE_SIZE); // Map entire file.
if (lpMapAddress == NULL)
{
printf("Could not map view of file.");
return ;
}
::Sleep(10000);
//Write data
HANDLE hEvent=::OpenEvent(
EVENT_MODIFY_STATE,
FALSE,
g_szEventName);
while(hEvent!=NULL){
scanf("%s",lpMapAddress);
::SetEvent(hEvent);
}
::CloseHandle(hEvent);
hEvent=INVALID_HANDLE_VALUE;
}
void Process2(){//读数据
hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, // Read/write permission.
FALSE, // Do not inherit the name
"MyFileMappingObject"); // of the mapping object.
if (hMapFile == NULL)
{
printf("Could not open file-mapping object.\n");
return;
}
lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.
FILE_MAP_ALL_ACCESS, // Read/write permission.
0, // Max. object size.
0, // Size of hFile.
FILE_SIZE); // Map entire file.
if (lpMapAddress == NULL)
{
printf("Could not map view of file.\n");
return;
}
//Wait for data
HANDLE hEvent=::CreateEvent(
NULL,
TRUE,
FALSE,
g_szEventName);
while(hEvent!=NULL){
::WaitForSingleObject(hEvent,INFINITE);
::ResetEvent(hEvent);
::Read();
}
::CloseHandle(hEvent);
hEvent=INVALID_HANDLE_VALUE;
}
void main(){
int flag;
cin>>flag;
if(flag==1){
Process1();
}
else if(flag==2){
Process2();
}
}