C++:共享内存(进程间通讯)(转载)

转载来自:http://blog.csdn.net/taily_duan/article/details/51692999

// ServerCom.cpp : Defines the entry point for the console application.  
02.//  
03.  
04.#include "stdafx.h"  
05.  
06.#include   
07.#include   
08.#pragma endregion  
09.#define MAP_PREFIX          L"Local\\"  
10.#define MAP_NAME            L"SampleMap"  
11.#define FULL_MAP_NAME       MAP_PREFIX MAP_NAME  
12.  
13.// Max size of the file mapping object.  
14.#define MAP_SIZE            65536  
15.  
16.// File offset where the view is to begin.  
17.#define VIEW_OFFSET         0  
18.  
19.// The number of bytes of a file mapping to map to the view. All bytes of the   
20.// view must be within the maximum size of the file mapping object (MAP_SIZE).   
21.// If VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to    
22.// the end of the file mapping.  
23.#define VIEW_SIZE           1024  
24.  
25.// Unicode string message to be written to the mapped view. Its size in byte   
26.// must be less than the view size (VIEW_SIZE).  
27.#define MESSAGE             L"Message from the first process."  
28.  
29.  
30.int _tmain(int argc, _TCHAR* argv[])  
31.{  
32.    HANDLE hMapFile = NULL;  
33.    PVOID pView = NULL;  
34.  
35.    // Create the file mapping object.  
36.    hMapFile = CreateFileMapping(  
37.        INVALID_HANDLE_VALUE,   // Use paging file - shared memory  
38.        NULL,                   // Default security attributes  
39.        PAGE_READWRITE,         // Allow read and write access  
40.        0,                      // High-order DWORD of file mapping max size  
41.        MAP_SIZE,               // Low-order DWORD of file mapping max size  
42.        FULL_MAP_NAME           // Name of the file mapping object  
43.        );  
44.    if (hMapFile == NULL)   
45.    {  
46.        wprintf(L"CreateFileMapping failed w/err 0x%08lx\n", GetLastError());  
47.        goto Cleanup;  
48.    }  
49.    wprintf(L"The file mapping (%s) is created\n", FULL_MAP_NAME);  
50.  
51.    // Map a view of the file mapping into the address space of the current   
52.    // process.  
53.    pView = MapViewOfFile(  
54.        hMapFile,               // Handle of the map object  
55.        FILE_MAP_ALL_ACCESS,    // Read and write access  
56.        0,                      // High-order DWORD of the file offset   
57.        VIEW_OFFSET,            // Low-order DWORD of the file offset   
58.        VIEW_SIZE               // The number of bytes to map to view  
59.        );  
60.    if (pView == NULL)  
61.    {   
62.        wprintf(L"MapViewOfFile failed w/err 0x%08lx\n", GetLastError());   
63.        goto Cleanup;  
64.    }  
65.    wprintf(L"The file view is mapped\n");  
66.  
67.    // Prepare a message to be written to the view.  
68.    PWSTR pszMessage = MESSAGE;  
69.    DWORD cbMessage = (wcslen(pszMessage) + 1) * sizeof(*pszMessage);  
70.  
71.    // Write the message to the view.  
72.    memcpy_s(pView, VIEW_SIZE, pszMessage, cbMessage);  
73.  
74.    wprintf(L"This message is written to the view:\n\"%s\"\n",  
75.        pszMessage);  
76.  
77.    // Wait to clean up resources and stop the process.  
78.    wprintf(L"Press ENTER to clean up resources and quit");  
79.    getchar();  
80.  
81.Cleanup:  
82.  
83.    if (hMapFile)  
84.    {  
85.        if (pView)  
86.        {  
87.            // Unmap the file view.  
88.            UnmapViewOfFile(pView);  
89.            pView = NULL;  
90.        }  
91.        // Close the file mapping object.  
92.        CloseHandle(hMapFile);  
93.        hMapFile = NULL;  
94.    }  
95.  
96.    return 0;  
97.}  

// ClientCom.cpp : Defines the entry point for the console application.  
02.//  
03.  
04.#include "stdafx.h"  
05.#include   
06.#include   
07.#pragma endregion  
08.#define MAP_PREFIX          L"Local\\"  
09.#define MAP_NAME            L"SampleMap"  
10.#define FULL_MAP_NAME       MAP_PREFIX MAP_NAME  
11.  
12.// File offset where the view is to begin.  
13.#define VIEW_OFFSET         0  
14.  
15.// The number of bytes of a file mapping to map to the view. All bytes of the   
16.// view must be within the maximum size of the file mapping object. If   
17.// VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to the   
18.// end of the file mapping.  
19.#define VIEW_SIZE           1024  
20.  
21.  
22.int _tmain(int argc, _TCHAR* argv[])  
23.{  
24.    HANDLE hMapFile = NULL;  
25.    PVOID pView = NULL;  
26.  
27.    // Try to open the named file mapping identified by the map name.  
28.    hMapFile = OpenFileMapping(  
29.        FILE_MAP_READ,          // Read access  
30.        FALSE,                  // Do not inherit the name  
31.        FULL_MAP_NAME           // File mapping name   
32.        );  
33.    if (hMapFile == NULL)   
34.    {  
35.        wprintf(L"OpenFileMapping failed w/err 0x%08lx\n", GetLastError());  
36.        goto Cleanup;  
37.    }  
38.    wprintf(L"The file mapping (%s) is opened\n", FULL_MAP_NAME);  
39.  
40.    // Map a view of the file mapping into the address space of the current   
41.    // process.  
42.    pView = MapViewOfFile(  
43.        hMapFile,               // Handle of the map object  
44.        FILE_MAP_READ,          // Read access  
45.        0,                      // High-order DWORD of the file offset   
46.        VIEW_OFFSET,            // Low-order DWORD of the file offset  
47.        VIEW_SIZE               // The number of bytes to map to view  
48.        );  
49.    if (pView == NULL)  
50.    {  
51.        wprintf(L"MapViewOfFile failed w/err 0x%08lx\n", GetLastError());   
52.        goto Cleanup;  
53.    }  
54.    wprintf(L"The file view is mapped\n");  
55.  
56.    // Read and display the content in view.  
57.    wprintf(L"Read from the file mapping:\n\"%s\"\n", (PWSTR)pView);  
58.  
59.    // Wait to clean up resources and stop the process.  
60.    wprintf(L"Press ENTER to clean up resources and quit");  
61.    getchar();  
62.  
63.Cleanup:  
64.  
65.    if (hMapFile)  
66.    {  
67.        if (pView)  
68.        {  
69.            // Unmap the file view.  
70.            UnmapViewOfFile(pView);  
71.            pView = NULL;  
72.        }  
73.        // Close the file mapping object.  
74.        CloseHandle(hMapFile);  
75.        hMapFile = NULL;  
76.    }  
77.  
78.    return 0;  
79.}  

你可能感兴趣的:(其他)