• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            統(tǒng)計(jì)

            • 隨筆 - 50
            • 文章 - 42
            • 評(píng)論 - 147
            • 引用 - 0

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 164705
            • 排名 - 159

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            創(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 CloseHandle function. When all handles are closed, the system can free the section of the paging file that the object uses.

            第一個(gè)進(jìn)程通過調(diào)用以INVALID_HANDLE_VALUE 為參數(shù)的CreateFileMapping方法創(chuàng)建一個(gè)命名的內(nèi)存映射文件對(duì)象。通過使用PAGE_READWRITE 標(biāo)識(shí),進(jìn)程可通過任何以創(chuàng)建文件視圖獲得文件讀寫權(quán)限。
            然后進(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ì)象所使用的頁面文件。
             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使用系統(tǒng)頁面文件
            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

            第二個(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

             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

            更多內(nèi)容參照http://baike.baidu.com/view/394293.htm

            posted on 2008-11-27 22:51 pear_li 閱讀(511) 評(píng)論(0)  編輯 收藏 引用 所屬分類: windows kernel

            亚洲精品第一综合99久久| 久久久久高潮综合影院| 久久人人爽人人爽人人片AV不| 内射无码专区久久亚洲| 国产高潮国产高潮久久久91 | 99久久精品免费看国产一区二区三区| 伊人色综合久久天天人手人婷| 久久笫一福利免费导航| 亚洲人成无码www久久久| 三级韩国一区久久二区综合| 中文字幕久久精品| 久久久久久久久波多野高潮| 久久精品一本到99热免费| 久久精品国产亚洲av麻豆蜜芽| 久久人人爽人人人人爽AV| 一本色道久久综合亚洲精品| 婷婷久久香蕉五月综合加勒比| 亚洲中文字幕无码一久久区| 久久久无码一区二区三区| 狠狠色丁香婷婷综合久久来| 成人亚洲欧美久久久久| 久久久精品国产Sm最大网站| 国产精品久久新婚兰兰| 漂亮人妻被黑人久久精品| 国产成人久久激情91| 狠狠色丁香婷婷综合久久来来去| 狠狠人妻久久久久久综合蜜桃| 一本久久精品一区二区| 久久99精品国产自在现线小黄鸭 | 久久精品国产半推半就| 久久精品一区二区三区中文字幕| 欧美成人免费观看久久| av国内精品久久久久影院| 九九热久久免费视频| 伊人久久大香线蕉成人| 精品无码久久久久久午夜| 欧美久久一级内射wwwwww.| 日韩AV无码久久一区二区| 99久久婷婷国产综合精品草原| 久久精品极品盛宴观看| 久久精品国产一区|