写入共享内存

发布于 2024-08-05 04:11:17 字数 668 浏览 2 评论 0原文

如何使用 Win32 API 将文件写入共享内存?

我有这样的代码:

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

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

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

  ReadFile(
  hFile,
  lpMapAddress,
  75,
  &bytesRead,
  NULL);

  sprintf((char*)lpMapAddress, "<output 1>");

  printf((char*) lpMapAddress);

但是, printf 调用仅返回“<输出 1 >”而不是文件的内容。

编辑: 发现问题了。当我调用 sprintf 时,我正在写入输入文件。但我还是不知道为什么...

How can I write from a file to shared memory using the Win32 API?

I have this code:

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

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

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

  ReadFile(
  hFile,
  lpMapAddress,
  75,
  &bytesRead,
  NULL);

  sprintf((char*)lpMapAddress, "<output 1>");

  printf((char*) lpMapAddress);

However, the printf call only returns "< output 1 >" and not the contents of the file.

EDIT:
Found the problem. I'm writing to the input file when I call sprintf. But I still don't know why...

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

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

发布评论

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

评论(4

萤火眠眠 2024-08-12 04:11:17

这是整个代码示例吗?在我看来,对 sprintf 的调用在 lpMapAddress 处放置了一个以 null 结尾的字符串,它有效地覆盖了您从文件中读取的任何内容 - 至少出于您的目的printf 语句。

如果您想用字符串 "" 替换您读取的内容的第一部分,您可以在读取文件后执行此操作:

char *tmp = "<output 1>";
strncpy((char*)lpMapAddress, tmp, strlen(tmp));

复制字符串的文本,但不复制其 null终结者。

Is this the entire code sample? It looks to me like the call to sprintf places a null-terminated string at lpMapAddress, which effectively overwrites whatever you read from the file--at least for the purposes of your printf statement.

If you want to replace the first part of what you read with the string "<output 1>", you could do this after reading the file:

char *tmp = "<output 1>";
strncpy((char*)lpMapAddress, tmp, strlen(tmp));

That copies the text of the string but not its null terminator.

一梦等七年七年为一梦 2024-08-12 04:11:17

sprintf 之后存储 NUL,并且 printf 在第一个 NUL 处停止。

(另外,将一些随机文件作为格式传递给 printf 也是一个坏主意。如果它包含 % 字符怎么办?但这是另一个问题。)

我正在写入输入文件
调用 sprintf。但我还是不知道
为什么...

因为这就是MapViewOfFile 所做的。它将文件的内容与内存块相关联。文件的当前内容出现在内存块中,对该内存所做的任何更改都会写入该文件。

The sprintf stores a NUL after <output 1>, and printf stops at the first NUL.

(Also, it's a bad idea to pass some random file as the format to printf. What if it contained % characters? But that's another issue.)

I'm writing to the input file when I
call sprintf. But I still don't know
why...

Because that's what MapViewOfFile does. It associates the file's contents with a block of memory. The current contents of the file appear in the memory block, and any changes you make to that memory are written to the file.

甜心 2024-08-12 04:11:17

我认为您不需要在映射后调用 ReadFile 。只需从 lpMapAddress 访问内容即可。

但是,对 MapViewOfFile 使用常量不会从使用内存文件映射中获益。

I don't think you need to call ReadFile after mapping. Just access the content from the lpMapAddress.

However, using constants for MapViewOfFile makes no benefit from using memory file mapping.

就是爱搞怪 2024-08-12 04:11:17

我真的不明白你在这里得到什么。你之前输入什么代码并不重要;最后一行总是返回您在前一个“sprintf”行中放入缓冲区的字符串。因为这是“”,所以这就是将返回的内容。

I don't really understand what you are getting at here. It doesn't matter what code you put before; That last line is always going to return the string you placed into the buffer in the previous "sprintf" line. Since that is "<output 1>", yes that's what will be returned.

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