C++进程间通信之共享内存
2017-09-28 15:27
435 查看
转载:http://blog.csdn.net/taily_duan/article/details/51692999
转载:http://blog.csdn.net/fengrx/article/details/4069088
转载:http://www.cnblogs.com/xuandi/p/5673917.html
注:运行的时候先运行写入的进程,再运行读出的进程
转载:http://blog.csdn.net/fengrx/article/details/4069088
转载:http://www.cnblogs.com/xuandi/p/5673917.html
// ServerCom.cpp : Defines the entry point for the console application. // #include "stdafx.h" #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." int _tmain(int argc, _TCHAR* argv[]) { HANDLE hMapFile = NULL; PVOID pView = NULL; // Create the file mapping object. hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // Use paging file - shared memory NULL, // Default security attributes PAGE_READWRITE, // Allow read and write access 0, // High-order DWORD of file mapping max size MAP_SIZE, // Low-order DWORD of file mapping max size FULL_MAP_NAME // Name of the file mapping object ); if (hMapFile == NULL) { wprintf(L"CreateFileMapping failed w/err 0x%08lx\n", GetLastError()); goto Cleanup; } wprintf(L"The file mapping (%s) is created\n", FULL_MAP_NAME); // Map a view of the file mapping into the address space of the current // process. pView = MapViewOfFile( hMapFile, // Handle of the map object FILE_MAP_ALL_ACCESS, // Read and write access 0, // High-order DWORD of the file offset VIEW_OFFSET, // Low-order DWORD of the file offset VIEW_SIZE // The number of bytes to map to view ); if (pView == NULL) { wprintf(L"MapViewOfFile failed w/err 0x%08lx\n", GetLastError()); goto Cleanup; } wprintf(L"The file view is mapped\n"); // Prepare a message to be written to the view. PWSTR pszMessage = MESSAGE; DWORD cbMessage = (wcslen(pszMessage) + 1) * sizeof(*pszMessage); // Write the message to the view. memcpy_s(pView, VIEW_SIZE, pszMessage, cbMessage); wprintf(L"This message is written to the view:\n\"%s\"\n", pszMessage); // Wait to clean up resources and stop the process. wprintf(L"Press ENTER to clean up resources and quit"); getchar(); Cleanup: if (hMapFile) { if (pView) { // Unmap the file view. UnmapViewOfFile(pView); pView = NULL; } // Close the file mapping object. CloseHandle(hMapFile); hMapFile = NULL; } return 0; }
// ClientCom.cpp : Defines the entry point for the console application. // #include "stdafx.h" #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 int _tmain(int argc, _TCHAR* argv[]) { HANDLE hMapFile = NULL; PVOID pView = NULL; // Try to open the named file mapping identified by the map name. hMapFile = OpenFileMapping( FILE_MAP_READ, // Read access FALSE, // Do not inherit the name FULL_MAP_NAME // File mapping name ); if (hMapFile == NULL) { wprintf(L"OpenFileMapping failed w/err 0x%08lx\n", GetLastError()); goto Cleanup; } wprintf(L"The file mapping (%s) is opened\n", FULL_MAP_NAME); // Map a view of the file mapping into the address space of the current // process. pView = MapViewOfFile( hMapFile, // Handle of the map object FILE_MAP_READ, // Read access 0, // High-order DWORD of the file offset VIEW_OFFSET, // Low-order DWORD of the file offset VIEW_SIZE // The number of bytes to map to view ); if (pView == NULL) { wprintf(L"MapViewOfFile failed w/err 0x%08lx\n", GetLastError()); goto Cleanup; } wprintf(L"The file view is mapped\n"); // Read and display the content in view. wprintf(L"Read from the file mapping:\n\"%s\"\n", (PWSTR)pView); // Wait to clean up resources and stop the process. wprintf(L"Press ENTER to clean up resources and quit"); getchar(); Cleanup: if (hMapFile) { if (pView) { // Unmap the file view. UnmapViewOfFile(pView); pView = NULL; } // Close the file mapping object. CloseHandle(hMapFile); hMapFile = NULL; } return 0; }
注:运行的时候先运行写入的进程,再运行读出的进程
相关文章推荐
- Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- Windows or Linux环境下利用“共享内存”实现进程间通信的C/C++代码
- [转]Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- C++虚拟继承中_对象内存的分布_虚继承会多余分配虚表v-tab的指针vptr_图1-1清楚的描述了虚继承类对象内存的分布_转载淘宝共享数据平台
- 进程间通信之共享内存
- Linux下网络编程(3)——进程间通信(IPC),共享内存,传递结构体数据
- Linux 进程间通信 - 共享内存shmget方式(转)
- Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存
- Windows进程间通信:共享内存
- win32下进程间通信——共享内存
- 进程间通信之:共享内存
- Linux环境进程间通信(五): 共享内存(上)
- Linux环境进程间通信: 共享内存
- /LGC设计模式/进程间通信之共享内存
- 进程间通信之共享内存(Share Memory)
- Linux进程间通信之共享内存
- Linux进程间通信:共享内存
- C++:共享内存(进程间通讯)(转载)