多个进程可以通过在系统页面文件中存储的内存映射文件来实现多个进程共享数据。
第一个进程
The first process creates the file mapping object by calling the CreateFileMapping function with INVALID_HANDLE_VALUE and a name for the object. By using the PAGE_READWRITE flag, the process has read/write permission to the memory through any file views that are created.
Then the process uses the file mapping object handle that CreateFileMapping returns in a call to MapViewOfFile to create a view of the file in the process address space. The MapViewOfFile function returns a pointer to the file view, pBuf. The process then uses the CopyMemory function to write a string to the view that can be accessed by other processes.
When the process no longer needs access to the file mapping object, it should call the CloseHandle function. When all handles are closed, the system can free the section of the paging file that the object uses.
第一个进程通过调用以INVALID_HANDLE_VALUE 为参数的CreateFileMapping方法创建一个命名的内存映射文件对象。通过使用PAGE_READWRITE 标识,进程可通过任何以创建文件视图获得文件读写权限。
然后进程使用
CreateFileMapping 返回的对象句柄作为参数调用
MapViewOfFile 方法在进程地址空间内创建一个文件视图。
MapViewOfFile 返回一个文件视图的指针。进程可以调用
CopyMemory 方法写一段字串到文件视图,而该视图可以被其他进程访问。
当进程不在需要访文件映射对象是,应该调用
CloseHandle 方法。当所有的进程都关闭后,系统会释放对象所使用的页面文件。
1#include <windows.h>
2#include <stdio.h>
3#include <conio.h>
4
5#define BUF_SIZE 256
6TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
7TCHAR szMsg[]=TEXT("Message from first process");
8
9int main()
10{
11 HANDLE hMapFile;
12 LPCTSTR pBuf;
13
14 hMapFile = CreateFileMapping(
15 INVALID_HANDLE_VALUE, // use paging file使用系统页面文件
16 NULL, // default security
17 PAGE_READWRITE, // read/write access
18 0, // max. object size
19 BUF_SIZE, // buffer size
20 szName); // name of mapping object
21
22 if (hMapFile == NULL)
23 {
24 printf("Could not create file mapping object (%d).\n",
25 GetLastError());
26 return 1;
27 }
28 pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
29 FILE_MAP_ALL_ACCESS, // read/write permission
30 0,
31 0,
32 BUF_SIZE);
33
34 if (pBuf == NULL)
35 {
36 printf("Could not map view of file (%d).\n",
37 GetLastError());
38 return 2;
39 }
40
41
42 CopyMemory((PVOID)pBuf, szMsg, strlen(szMsg));
43 _getch();
44
45 UnmapViewOfFile(pBuf);
46
47 CloseHandle(hMapFile);
48
49 return 0;
50}
51
第二个进程
A second process can access the string written to the shared memory by the first process by calling the OpenFileMapping function specifying the same name for the mapping object as the first process. Then it can use the MapViewOfFile function to obtain a pointer to the file view, pBuf. The process can display this string as it would any other string. In this example, the message box displayed contains the message "Message from first process" that was written by the first process.
第二个进程可以通过OpenFileMapping 方法并且指定与第一个进程中相同的内存映射文件对象名称来访问共享内存中的字串。然后可通过MapViewOfFile 方法得到文件视图的指针pBuf
1#include <windows.h>
2#include <stdio.h>
3#include <conio.h>
4
5#define BUF_SIZE 256
6TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
7
8int main()
9{
10 HANDLE hMapFile;
11 LPCTSTR pBuf;
12
13 hMapFile = OpenFileMapping(
14 FILE_MAP_ALL_ACCESS, // read/write access
15 FALSE, // do not inherit the name
16 szName); // name of mapping object
17
18 if (hMapFile == NULL)
19 {
20 printf("Could not open file mapping object (%d).\n",
21 GetLastError());
22 return 1;
23 }
24
25 pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
26 FILE_MAP_ALL_ACCESS, // read/write permission
27 0,
28 0,
29 BUF_SIZE);
30
31 if (pBuf == NULL)
32 {
33 printf("Could not map view of file (%d).\n",
34 GetLastError());
35 return 2;
36 }
37
38 MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
39
40 UnmapViewOfFile(pBuf);
41
42 CloseHandle(hMapFile);
43
44 return 0;
45}
46
更多内容参照
http://baike.baidu.com/view/394293.htm