如何在 C++ 中写入共享内存?

发布于 2024-08-04 21:55:03 字数 830 浏览 2 评论 0原文

我想写入共享内存,然后将内容转储到 win32 api 中的文件中。 目前我有这样的代码:

HANDLE hFile, hMapFile;
  LPVOID lpMapAddress;

  hFile = CreateFile("input.map",
  GENERIC_READ | GENERIC_WRITE,
  0,
  NULL,
  OPEN_EXISTING,
  FILE_ATTRIBUTE_NORMAL,
  NULL);

  hMapFile = CreateFileMapping(hFile,
  NULL,
  PAGE_READWRITE,
  0,
  0,
  TEXT("SharedObject"));

  lpMapAddress = MapViewOfFile(hMapFile,
  FILE_MAP_ALL_ACCESS,
  0,
  0,
  0);

  sprintf(MapViewOfFile, "<output 1>");

  UnmapViewOfFile(lpMapAddress);
  CloseHandle(hFile);
  CloseHandle(hMapFile);

但是,第 31 行( sprintf 调用)给出了错误:

error: cannot convert `void*(*)(void*, DWORD, DWORD, DWORD, DWORD)' 
to `char*' for argument `1' to `int sprintf(char*, const char*, ...)'

我尝试将 lpMapAddress 转换为 LPTSTR ,但它没有效果。我做错了什么?或者有更好的方法吗?

I'd like to write to shared memory and then dump the contents to a file in the win32 api.
Currently I have this code:

HANDLE hFile, hMapFile;
  LPVOID lpMapAddress;

  hFile = CreateFile("input.map",
  GENERIC_READ | GENERIC_WRITE,
  0,
  NULL,
  OPEN_EXISTING,
  FILE_ATTRIBUTE_NORMAL,
  NULL);

  hMapFile = CreateFileMapping(hFile,
  NULL,
  PAGE_READWRITE,
  0,
  0,
  TEXT("SharedObject"));

  lpMapAddress = MapViewOfFile(hMapFile,
  FILE_MAP_ALL_ACCESS,
  0,
  0,
  0);

  sprintf(MapViewOfFile, "<output 1>");

  UnmapViewOfFile(lpMapAddress);
  CloseHandle(hFile);
  CloseHandle(hMapFile);

However, line 31 (the sprintf call) gives the error:

error: cannot convert `void*(*)(void*, DWORD, DWORD, DWORD, DWORD)' 
to `char*' for argument `1' to `int sprintf(char*, const char*, ...)'

I've tried casting the lpMapAddress to LPTSTR, but it has no effect. What am I doing wrong? Or is there a better way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心如荒岛 2024-08-11 21:55:03

在该

sprintf(MapViewOfFile, "<output 1>"); 

行中,您需要 lpMapAddress,而不是 MapViewOfFile。或者准确地说是 (char*)lpMapAddress

In the

sprintf(MapViewOfFile, "<output 1>"); 

line, you wanted lpMapAddress, not MapViewOfFile. Or (char*)lpMapAddress to be precise.

别想她 2024-08-11 21:55:03

您正在尝试写入常规文件。要写入共享内存,您应该将 INVALID_HANDLE_VALUE 传递给 CreateFileMapping。请参阅这篇文章了解更多详细信息。

   TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
   HANDLE hMapFile;
   PVOID pBuf;
   const DWORD BUF_SIZE = 256;

   hMapFile = CreateFileMapping(
                 INVALID_HANDLE_VALUE,    // use paging file
                 NULL,                    // default security 
                 PAGE_READWRITE,          // read/write access
                 0,                       // max. object size 
                 BUF_SIZE,                // buffer size  
                 szName);                 // name of mapping object

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

   TCHAR szMsg[]=TEXT("<output 1>");
   CopyMemory(pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));

   UnmapViewOfFile(pBuf);    
   CloseHandle(hMapFile);

You are trying to write to a regular file. To write to shared memory you should pass INVALID_HANDLE_VALUE to CreateFileMapping. Check out this article for more details.

   TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
   HANDLE hMapFile;
   PVOID pBuf;
   const DWORD BUF_SIZE = 256;

   hMapFile = CreateFileMapping(
                 INVALID_HANDLE_VALUE,    // use paging file
                 NULL,                    // default security 
                 PAGE_READWRITE,          // read/write access
                 0,                       // max. object size 
                 BUF_SIZE,                // buffer size  
                 szName);                 // name of mapping object

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

   TCHAR szMsg[]=TEXT("<output 1>");
   CopyMemory(pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));

   UnmapViewOfFile(pBuf);    
   CloseHandle(hMapFile);
鹿港巷口少年归 2024-08-11 21:55:03

在 sprintf 中,您传递函数 MapViewOfFile 的地址。相反,您应该传递映射的内存地址。

In sprintf you are passing the address of the function MapViewOfFile. Instead you should pass the mapped memory address.

倾城花音 2024-08-11 21:55:03

回答“有更好的方法吗?”:看看 Boost.Interprocess内存映射部分尤其如此。

Answering the "is there a better way to do it?": Take a look at Boost.Interprocess, the section on memory mapping in particular.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文