内存映射文件 修改以及保存文件
/**************************************************************** // Function : 内存映射文件进行文件同步操作的例子,转载请注明出处 // Author : Lthis // Create : 2015-2-1 11:07:50 // Checked : Lthis 2015-2-1 ****************************************************************/ #include "stdafx.h" #include "windows.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { /* code by Lthis */ HANDLE hFile = ::CreateFile(L"C:\\tmp\\a.exe", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); HANDLE hFileMapping = ::CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL); PBYTE pbFile = NULL; pbFile = (PBYTE)::MapViewOfFile(hFileMapping, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0); cout << pbFile << endl; pbFile[0] = '!'; pbFile[1] = '@'; FlushViewOfFile(pbFile, 2); //禁止写延迟,立即写入 cout << pbFile << endl; ::UnmapViewOfFile(pbFile); ::CloseHandle(hFileMapping); ::CloseHandle(hFile); /* code by Lthis */ getchar(); return 0; }