0

我想为所有其他进程创建一个共享内存池来共享数据,但是在阅读了 CreateFileMapping API 文档后,我对它需要指定共享内存的大小感到困惑。我实际上希望它是动态分配和免费的,它看起来更像是一项服务。有没有办法使用 createFileMapping 来动态共享内存?

4

1 回答 1

0

创建命名共享内存。

第一道工序

第一个过程通过调用CreateFileMapping带有对象名称的函数来创建文件映射INVALID_HANDLE_VALUE对象。通过使用该PAGE_READWRITE标志,该进程通过创建的任何文件视图具有对内存的读/写权限。CreateFileMapping然后进程使用在调用中返回的文件映射对象句柄MapViewOfFile在进程地址空间中创建文件视图。该MapViewOfFile函数返回一个指向文件视图的指针,pBuf. 然后,该进程使用 CopyMemory 函数将字符串写入视图,以供其他进程访问。

流程1代码:

#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. 该进程可以像显示任何其他字符串一样显示此字符串。在此示例中,显示的消息框包含由第一个进程编写的消息“来自第一个进程的消息”。

过程2代码:

#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;
}

来源: http: //msdn.microsoft.com/en-us/library/windows/desktop/aa366551 (v=vs.85).aspx

于 2011-12-27T16:22:00.500 回答