简体   繁体   English

写入共享内存

[英]Writing to shared memory

How can I write from a file to shared memory using the Win32 API? 如何使用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. 但是,printf调用仅返回“ <输出1>”,而不返回文件的内容。

EDIT: Found the problem. 编辑:发现了问题。 I'm writing to the input file when I call sprintf. 调用sprintf时,我正在写输入文件。 But I still don't know why... 但是我还是不知道为什么

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. 在我看来,对sprintf的调用在lpMapAddress处放置了一个以空值终止的字符串,这有效地覆盖了您从文件中读取的所有内容-至少出于printf语句的目的。

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: 如果要用字符串"<output 1>"替换所读取内容的第一部分,则可以在读取文件后执行以下操作:

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

That copies the text of the string but not its null terminator. 它将复制字符串的文本,但不复制其空终止符。

The sprintf stores a NUL after <output 1> , and printf stops at the first NUL. sprintf<output 1>之后存储一个NUL,而printf在第一个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.) (另外,将一些随机文件作为格式传递给printf是一个坏主意。如果它包含%字符怎么办?但这是另一个问题。)

I'm writing to the input file when I call sprintf. 调用sprintf时,我正在写输入文件。 But I still don't know why... 但是我还是不知道为什么

Because that's what MapViewOfFile does . 因为那是MapViewOfFile 所做的 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. 文件的当前内容出现在内存块中,并且您对该内存所做的任何更改都将写入文件中。

I don't think you need to call ReadFile after mapping. 我认为映射后不需要调用ReadFile。 Just access the content from the lpMapAddress. 只需从lpMapAddress访问内容。

However, using constants for MapViewOfFile makes no benefit from using memory file mapping. 但是,将常量用于MapViewOfFile不会从使用内存文件映射中受益。

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. 最后一行始终将返回您在上一个“ sprintf”行中放入缓冲区的字符串。 Since that is " <output 1> ", yes that's what will be returned. 由于那是“ <output 1> ”,是的,这就是要返回的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM