// Writer.cpp #include"stdafx.h" #include <windows.h> #include <conio.h> #define BUF_SIZE 256 wchar_t szName[30]=TEXT("Global\\MyFileMappingObject"); wchar_t szMsg[30]=TEXT("Message from first process."); wchar_t chr[100]; int _tmain() { HANDLE hMapFile; LPCTSTR pBuf; hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, // maximum object size (high-order DWORD) BUF_SIZE, // maximum object size (low-order DWORD) (LPCTSTR)szName); // name of mapping object if (hMapFile == NULL) { _tprintf(TEXT("Could not create file mapping object (%d).\n"), GetLastError()); return 1; } pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object FILE_MAP_WRITE, // read/write permission 0, 0, BUF_SIZE); if (pBuf == NULL) { _tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError()); CloseHandle(hMapFile); return 1; } printf("input transform data:"); scanf("%s",szMsg); //_tscanf("%s",szMsg); CopyMemory((PVOID)pBuf, szMsg, (wcslen(szMsg) * sizeof(wchar_t))); //scanf("%s"df _getch(); UnmapViewOfFile(pBuf); CloseHandle(hMapFile); _tprintf(TEXT("success")); //system("pause"); //_getch(); return 0; }
//Reader.cpp // #include "stdafx.h" #include <windows.h> #include <stdio.h> #include <conio.h> #include <tchar.h> #include<cstdlib> #pragma comment(lib, "user32.lib") #define BUF_SIZE 256 wchar_t szName[]=TEXT("Global\\MyFileMappingObject"); int _tmain() { HANDLE hMapFile; LPCTSTR pBuf; hMapFile = OpenFileMapping( FILE_MAP_ALL_ACCESS, // read/write access FALSE, // do not inherit the name (LPCTSTR)szName); // name of mapping object if (hMapFile == NULL) { _tprintf(TEXT("Could not open file mapping object (%d).\n"),GetLastError()); return 1; } pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object FILE_MAP_READ, // read/write permission 0, 0, BUF_SIZE); if (pBuf == NULL) { _tprintf(TEXT("Could not map view of file (%d).\n"),GetLastError()); CloseHandle(hMapFile); return 1; } printf("%s\n",pBuf); system("pause"); UnmapViewOfFile(pBuf); CloseHandle(hMapFile); return 0; }