// MapFile.cpp : Defines the entry point for the console application. //http://msdn.microsoft.com/en-us/library/ms810613.aspx #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <windows.h> int _tmain(int argc, _TCHAR* argv[]) { OFSTRUCT opBuf; HANDLE hMapfile; HANDLE hMapview; BYTE *recv; HANDLE hFile = CreateFile(L".//map.test", FILE_GENERIC_READ|FILE_GENERIC_WRITE, / FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, NULL, NULL); if (hFile == NULL) { printf("create file failed!/n"); return 1; } //if hFile is 0xffffffff, it is for process share hMapfile=CreateFileMapping((HANDLE)hFile,NULL,PAGE_READWRITE,0,0x01400000,L"MapTest"); //0x4000000 0x01400000 /* 10 can not be 0, or you can not CreateFileMap successfully another thing: if you know that the hFile size is not zero(there are chars in the file), then 10 can be set 0 */ if(hMapfile==NULL) { printf("create mapping file failed!/n"); printf("%d/n", GetLastError()); return 1; } CloseHandle((HANDLE)hFile); hFile=0; hMapview=MapViewOfFile(hMapfile,FILE_MAP_WRITE,0,0,0); if(hMapview==NULL) { printf("mapping view failed!/n"); return 1; } recv=(BYTE *)hMapview; printf("Mapping view's content is :%.10s /n",recv); recv[0]='I';recv[1]=' ';recv[2]='l';recv[3]='o';recv[4]='v'; recv[5]='e';recv[6]=' ';recv[7]='y';recv[8]='o';recv[9]='u'; //_tcscpy((wchar_t *)recv, L"I love you!"); printf("Mapping view's content is :%.10s /n",recv); UnmapViewOfFile(hMapview); return 0; }