// Shared.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
class Map{
private:
HANDLE FileMap;
PVOID pView;
public:
Map(){}
~Map(){
CloseHandle(FileMap);
UnmapViewOfFile(pView);
}
BOOL CREATE(){
FileMap = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,4*1024,TEXT("Shared"));
if(FileMap != NULL){
if(GetLastError() == ERROR_ALREADY_EXISTS){
_tprintf(TEXT("Already Mapped!\n"));
return FALSE;
}else{
pView = MapViewOfFile(FileMap,FILE_MAP_READ|FILE_MAP_WRITE,0,0,4*1024);
wcscpy_s((wchar_t *)pView,10,TEXT("Hello"));
return TRUE;
}
}else{
return 0;
}
}
BOOL read(){
FileMap = OpenFileMapping(FILE_MAP_READ|FILE_MAP_WRITE,FALSE,TEXT("Shared"));
if(FileMap != NULL){
pView = MapViewOfFile(FileMap,FILE_MAP_READ|FILE_MAP_WRITE,0,0,4*1024);
_tprintf(TEXT("Ha:%s\n"),pView);
return TRUE;
}else{
_tprintf(TEXT("Can't Find\n"));
return 0;
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Map mi;
char a;
cin>>a;
while(a != 'q'){
switch(a){
case '1':
mi.CREATE();
break;
case '2':
mi.read();
break;
}
getchar();
cin>>a;
}
return 0;
}
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
class Map{
private:
HANDLE FileMap;
PVOID pView;
public:
Map(){}
~Map(){
CloseHandle(FileMap);
UnmapViewOfFile(pView);
}
BOOL CREATE(){
FileMap = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,4*1024,TEXT("Shared"));
if(FileMap != NULL){
if(GetLastError() == ERROR_ALREADY_EXISTS){
_tprintf(TEXT("Already Mapped!\n"));
return FALSE;
}else{
pView = MapViewOfFile(FileMap,FILE_MAP_READ|FILE_MAP_WRITE,0,0,4*1024);
wcscpy_s((wchar_t *)pView,10,TEXT("Hello"));
return TRUE;
}
}else{
return 0;
}
}
BOOL read(){
FileMap = OpenFileMapping(FILE_MAP_READ|FILE_MAP_WRITE,FALSE,TEXT("Shared"));
if(FileMap != NULL){
pView = MapViewOfFile(FileMap,FILE_MAP_READ|FILE_MAP_WRITE,0,0,4*1024);
_tprintf(TEXT("Ha:%s\n"),pView);
return TRUE;
}else{
_tprintf(TEXT("Can't Find\n"));
return 0;
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Map mi;
char a;
cin>>a;
while(a != 'q'){
switch(a){
case '1':
mi.CREATE();
break;
case '2':
mi.read();
break;
}
getchar();
cin>>a;
}
return 0;
}