========================================================================
通过文件映射对象共享内存 1.1往共享内存里写数据 // ServerCom.cpp : Defines the entry point for the console application. #include <stdio.h> #include <windows.h> #pragma endregion #define MAP_PREFIX L"Local\\" #define MAP_NAME L"SampleMap" #define FULL_MAP_NAME MAP_PREFIX MAP_NAME // Max size of the file mapping object. #define MAP_SIZE 65536 // File offset where the view is to begin. #define VIEW_OFFSET 0 // The number of bytes of a file mapping to map to the view. All bytes of the // view must be within the maximum size of the file mapping object (MAP_SIZE). // If VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to // the end of the file mapping. #define VIEW_SIZE 1024 // Unicode string message to be written to the mapped view. Its size in byte // must be less than the view size (VIEW_SIZE). #define MESSAGE L"Message from the first process." // 实现不同的进程共享数据; //1、要先使用函数CreateFileMapping来创建一个想共享的文件数据句柄; //2、使用MapViewOfFile来获取共享的内存地址; //3、使用OpenFileMapping函数在另一个进程里打开共享文件的名称; int main(int argc, char* argv[]) { HANDLE hMapFile = NULL; PVOID pView = NULL; // Create the file mapping object. hMapFile = CreateFileMapping(//创建一个想共享的文件数据句柄; INVALID_HANDLE_VALUE, // 表示在页面文件中创建一个可共享的文件映射; NULL, // 表示使用默认安全对象; PAGE_READWRITE, // 以可读、可写方式打开映射; 0, // 文件映射的最大长度的高32位; MAP_SIZE, // 文件映射的最大长度的低32位; FULL_MAP_NAME // 指定文件映射对象的名字; ); if (hMapFile == NULL) { wprintf(L"CreateFileMapping failed w/err 0xlx\n", GetLastError()); goto Cleanup; } wprintf(L"The file mapping (%s) is created\n", FULL_MAP_NAME); // 将一个文件映射对象映射到当前应用程序的地址空间; pView = MapViewOfFile(//使用MapViewOfFile来获取共享的内存地址; hMapFile, // 文件映像对象句柄; FILE_MAP_ALL_ACCESS, // 映射对象的文方件数据的访问式; 0, // 文件映射起始偏移的高32位; VIEW_OFFSET, // 表示文件映射起始偏移的低32位; VIEW_SIZE // 映射文件的字节数; ); if (pView == NULL) { wprintf(L"MapViewOfFile failed w/err 0xlx\n", GetLastError()); goto Cleanup; } wprintf(L"The file view is mapped\n"); PWSTR pszMessage = MESSAGE; DWORD cbMessage = (wcslen(pszMessage) + 1) * sizeof(*pszMessage); memcpy_s(pView, VIEW_SIZE, pszMessage, cbMessage);//*********写数据; wprintf(L"This message is written to the view:\n\"%s\"\n", pszMessage); wprintf(L"Press ENTER to clean up resources and quit"); getchar(); Cleanup: if (hMapFile) { if (pView) { UnmapViewOfFile(pView);//是停止当前程序的一个内存映射;在当前应用程序的内存地址空间解除对一个文件映射对象的映射; pView = NULL; } // 关闭文件映射对象; CloseHandle(hMapFile); hMapFile = NULL; } return 0; }1.2、从共享内存里读数据
// ClientCom.cpp : Defines the entry point for the console application. #pragma region #include <stdio.h> #include <windows.h> #pragma endregion //来定义可以扩展和收缩的代码区域的结尾; #define MAP_PREFIX L"Local\\" #define MAP_NAME L"SampleMap" #define FULL_MAP_NAME MAP_PREFIX MAP_NAME // File offset where the view is to begin. #define VIEW_OFFSET 0 // The number of bytes of a file mapping to map to the view. All bytes of the // view must be within the maximum size of the file mapping object. If // VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to the // end of the file mapping. #define VIEW_SIZE 1024 // 实现不同的进程共享数据; //1、要先使用函数CreateFileMapping来创建一个想共享的文件数据句柄; //2、使用MapViewOfFile来获取共享的内存地址; //3、使用OpenFileMapping函数在另一个进程里打开共享文件的名称; int _tmain(int argc, char* argv[]) { HANDLE hMapFile = NULL; PVOID pView = NULL; hMapFile = OpenFileMapping(//打开一个现成的文件映射对象的函数; FILE_MAP_READ, // Read access FALSE, // 如这个函数返回的句柄能由当前进程启动的新进程继承,则这个参数为TRUE; FULL_MAP_NAME // 指定要打开的文件映射对象名称; ); if (hMapFile == NULL) { wprintf(L"OpenFileMapping failed w/err 0xlx\n", GetLastError()); goto Cleanup; } wprintf(L"The file mapping (%s) is opened\n", FULL_MAP_NAME); // 将一个文件映射对象映射到当前应用程序的地址空间; pView = MapViewOfFile( hMapFile, // 文件映像对象句柄; FILE_MAP_READ, // 映射对象的文件数据的访问式; 0, // 文件映射起始偏移的高32位; VIEW_OFFSET, // 表示文件映射起始偏移的低32位; VIEW_SIZE // 映射文件的字节数; ); if (pView == NULL) { wprintf(L"MapViewOfFile failed w/err 0xlx\n", GetLastError()); goto Cleanup; } wprintf(L"The file view is mapped\n"); wprintf(L"Read from the file mapping:\n\"%s\"\n", (PWSTR)pView);//*****读数据; wprintf(L"Press ENTER to clean up resources and quit"); getchar(); Cleanup: if (hMapFile) { if (pView) { //是停止当前程序的一个内存映射;在当前应用程序的内存地址空间解除对一个文件映射对象的映射; UnmapViewOfFile(pView); pView = NULL; } // 关闭文件映射对象; CloseHandle(hMapFile); hMapFile = NULL; } return 0; }