gpt4 book ai didi

c++ - MapViewOfFile 在 32 位和 64 位进程之间共享

转载 作者:可可西里 更新时间:2023-11-01 13:08:09 28 4
gpt4 key购买 nike

我正在尝试在 64 位进程中对已映射到另一个 32 位进程内存的文件使用 MapViewOfFile。它失败并给我一个“拒绝访问”错误。这是一个已知的 Windows 限制还是我做错了什么?相同的代码适用于 2 个 32 位进程。

代码大概是这样的:

hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szShmName);
if (NULL == hMapFile)
{ /* failed to open - create new (this happens in the 32 bit app) */
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = FALSE;
/* give access to members of administrators group */
BOOL success = ConvertStringSecurityDescriptorToSecurityDescriptor(
"D:(A;OICI;GA;;;BA)",
SDDL_REVISION_1,
&(sa.lpSecurityDescriptor),
NULL);
HANDLE hShmFile = CreateFile(FILE_XXX_SHM,
FILE_ALL_ACCESS, 0,
&sa,
OPEN_ALWAYS, 0, NULL);

hMapFile = CreateFileMapping(hShmFile, &sa, PAGE_READWRITE,
0,
SHM_SIZE,
szShmName);

CloseHandle(hShmFile);
}

// this one fails in 64 bit app
pShm = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, SHM_SIZE);

最佳答案

当您在 32 位应用程序中调用 CreateFile 时,您将传递 0 作为共享参数,这意味着不允许共享。将其更改为 FILE_SHARE_READ | FiLE_SHARE_WRITE 可能是朝着正确方向迈出的一步。

编辑:我刚刚整理了一个有效的演示(至少对我而言):

#include <windows.h>
#include <iostream>

static const char map_name[] = "FileMapping1";
static const char event1_name[] = "EventName1";
static const char event2_name[] = "EventName2";

int main() {
HANDLE mapping = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, map_name);

if (NULL == mapping) {
std::cout << "Calling CreateFile\n";
HANDLE file = CreateFile("MappedFile",
FILE_ALL_ACCESS,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
0,
NULL);
std::cout << "Creating File mapping\n";
mapping = CreateFileMapping(file, NULL, PAGE_READWRITE, 0, 65536, map_name);

std::cout << "Closing file handle\n";
CloseHandle(file);
}

std::cout << "Mapping view of file\n";
char *memory = (char *)MapViewOfFile(mapping, FILE_MAP_ALL_ACCESS, 0, 0, 65536);
if (memory == NULL) {
std::cerr << "Mapping Failed.\n";
return 1;
}
std::cout << "Mapping succeeded\n";

HANDLE event = CreateEvent(NULL, false, false, event1_name);

if (GetLastError()==ERROR_ALREADY_EXISTS) {
std::cout <<"Waiting to receive string:\n";
WaitForSingleObject(event, INFINITE);
std::cout << "Received: " << memory;
HANDLE event2 = CreateEvent(NULL, false, false, event2_name);
SetEvent(event2);
}
else {
char string[] = "This is the shared string";
std::cout << "Sending string: " << string << "\n";
strncpy(memory, string, sizeof(string));
SetEvent(event);
HANDLE event2 = CreateEvent(NULL, false, false, event2_name);
WaitForSingleObject(event2, INFINITE);
}
return 0;
}

32 位或 64 位可执行文件的任何组合似乎都可以正常工作。

Edit2:但是请注意,这是纯粹的演示级代码。例如,每个共享对象的名称通常应包含一个 GUID 字符串,以确保不会与其他程序发生意外冲突。我还跳过了相当多的错误检查,更不用说这段代码没有完成任何有用的细节。

关于c++ - MapViewOfFile 在 32 位和 64 位进程之间共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2455146/

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