gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:59:05 24 4
gpt4 key购买 nike

我想写入共享内存,然后将内容转储到 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,但没有效果。我究竟做错了什么?或者有更好的方法吗?

最佳答案

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

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

关于c++ - 如何在 C++ 中写入共享内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1423031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com