創(chuàng)建一塊命名的進(jìn)程共享存儲(chǔ)空間
多個(gè)進(jìn)程可以通過在系統(tǒng)頁面文件中存儲(chǔ)的內(nèi)存映射文件來實(shí)現(xiàn)多個(gè)進(jìn)程共享數(shù)據(jù)。
第一個(gè)進(jìn)程
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
然后進(jìn)程使用CreateFileMapping 返回的對(duì)象句柄作為參數(shù)調(diào)用MapViewOfFile 方法在進(jìn)程地址空間內(nèi)創(chuàng)建一個(gè)文件視圖。MapViewOfFile 返回一個(gè)文件視圖的指針。進(jìn)程可以調(diào)用CopyMemory 方法寫一段字串到文件視圖,而該視圖可以被其他進(jìn)程訪問。
當(dāng)進(jìn)程不在需要訪文件映射對(duì)象是,應(yīng)該調(diào)用CloseHandle 方法。當(dāng)所有的進(jìn)程都關(guān)閉后,系統(tǒng)會(huì)釋放對(duì)象所使用的頁面文件。

2

3

4

5

6

7

8

9

10



11

12

13

14

15

16

17

18

19

20

21

22

23



24

25

26

27

28

29

30

31

32

33

34

35



36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

第二個(gè)進(jìn)程
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.
第二個(gè)進(jìn)程可以通過OpenFileMapping 方法并且指定與第一個(gè)進(jìn)程中相同的內(nèi)存映射文件對(duì)象名稱來訪問共享內(nèi)存中的字串。然后可通過MapViewOfFile 方法得到文件視圖的指針pBuf

2

3

4

5

6

7

8

9



10

11

12

13

14

15

16

17

18

19



20

21

22

23

24

25

26

27

28

29

30

31

32



33

34

35

36

37

38

39

40

41

42

43

44

45

46

更多內(nèi)容參照http://baike.baidu.com/view/394293.htm
posted on 2008-11-27 22:51 pear_li 閱讀(511) 評(píng)論(0) 編輯 收藏 引用 所屬分類: windows kernel