建立具名共享記憶體

若要共享數據,多個進程可以使用系統分頁檔案所儲存的記憶體對應檔案。

注意

此範例中的程式代碼在運行時間需要系統管理許可權。

第一個程式

第一個程式會呼叫 CreateFileMapping 函式,並使用 INVALID_HANDLE_VALUE 和 對象的名稱來建立檔案對應物件。 透過使用 PAGE_READWRITE 旗標,進程會透過任何建立的檔案檢視,擁有記憶體的讀取/寫入許可權。

然後進程會使用 CreateFileMapping 在呼叫 MapViewOfFile 時傳回的檔案對應物件句柄,在行程地址空間中建立檔案的檢視。 MapViewOfFile 函式會傳回檔案檢視的指標。 pBuf 然後,此程式會 使用 CopyMemory 函式,將字串寫入其他進程可以存取的檢視。

在檔案對應物件名稱前面加上 「Global\」 可讓進程彼此通訊,即使它們位於不同的終端伺服器會話中也一樣。 這需要第一個進程必須具有 SeCreateGlobalPrivilege 許可權。

當進程不再需要存取檔案對應物件時,它應該呼叫 CloseHandle 函 式。 關閉所有句柄時,系統可以釋放物件所使用的分頁檔案區段。

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>

#define BUF_SIZE 256
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
TCHAR szMsg[]=TEXT("Message from first process.");

int _tmain()
{
   HANDLE hMapFile;
   LPCTSTR pBuf;

   hMapFile = CreateFileMapping(
                 INVALID_HANDLE_VALUE,    // use paging file
                 NULL,                    // default security
                 PAGE_READWRITE,          // read/write access
                 0,                       // maximum object size (high-order DWORD)
                 BUF_SIZE,                // maximum object size (low-order DWORD)
                 szName);                 // name of mapping object

   if (hMapFile == NULL)
   {
      _tprintf(TEXT("Could not create file mapping object (%d).\n"),
             GetLastError());
      return 1;
   }
   pBuf = (LPTSTR) MapViewOfFile(hMapFile,   // handle to map object
                        FILE_MAP_ALL_ACCESS, // read/write permission
                        0,
                        0,
                        BUF_SIZE);

   if (pBuf == NULL)
   {
      _tprintf(TEXT("Could not map view of file (%d).\n"),
             GetLastError());

       CloseHandle(hMapFile);

      return 1;
   }


   CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
    _getch();

   UnmapViewOfFile(pBuf);

   CloseHandle(hMapFile);

   return 0;
}

第二個程式

第二個進程可以呼叫 OpenFileMapping 函式,為對應物件指定與第一個進程相同的名稱,以存取第一個進程寫入至共用記憶體的字串。 然後,它可以使用 MapViewOfFile 函式來取得檔案檢視的指標。 pBuf 進程可以顯示此字串,就像任何其他字串一樣。 在此範例中,所顯示的消息框包含第一個進程所撰寫的訊息「來自第一個進程的訊息」。

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")

#define BUF_SIZE 256
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");

int _tmain()
{
   HANDLE hMapFile;
   LPCTSTR pBuf;

   hMapFile = OpenFileMapping(
                   FILE_MAP_ALL_ACCESS,   // read/write access
                   FALSE,                 // do not inherit the name
                   szName);               // name of mapping object

   if (hMapFile == NULL)
   {
      _tprintf(TEXT("Could not open file mapping object (%d).\n"),
             GetLastError());
      return 1;
   }

   pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
               FILE_MAP_ALL_ACCESS,  // read/write permission
               0,
               0,
               BUF_SIZE);

   if (pBuf == NULL)
   {
      _tprintf(TEXT("Could not map view of file (%d).\n"),
             GetLastError());

      CloseHandle(hMapFile);

      return 1;
   }

   MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);

   UnmapViewOfFile(pBuf);

   CloseHandle(hMapFile);

   return 0;
}

共用檔案和記憶體