写入共享内存
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是整个代码示例吗?在我看来,对
sprintf
的调用在lpMapAddress
处放置了一个以 null 结尾的字符串,它有效地覆盖了您从文件中读取的任何内容 - 至少出于您的目的printf
语句。如果您想用字符串
"
替换您读取的内容的第一部分,您可以在读取文件后执行此操作:复制字符串的文本,但不复制其 null终结者。
Is this the entire code sample? It looks to me like the call to
sprintf
places a null-terminated string atlpMapAddress
, which effectively overwrites whatever you read from the file--at least for the purposes of yourprintf
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:That copies the text of the string but not its null terminator.
sprintf
在之后存储 NUL,并且
printf
在第一个 NUL 处停止。(另外,将一些随机文件作为格式传递给
printf
也是一个坏主意。如果它包含%
字符怎么办?但这是另一个问题。)因为这就是
MapViewOfFile
所做的。它将文件的内容与内存块相关联。文件的当前内容出现在内存块中,对该内存所做的任何更改都会写入该文件。The
sprintf
stores a NUL after<output 1>
, andprintf
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.)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.我认为您不需要在映射后调用 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.
我真的不明白你在这里得到什么。你之前输入什么代码并不重要;最后一行总是返回您在前一个“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.