两个进程之间的共享映射内存在编辑时未更新

the shared mapped memory between two processes in't updated when it's edited

本文关键字:存在 内存 映射 编辑 更新 共享 两个 之间 进程      更新时间:2023-10-16

我使用C 在Windows上,并制作一种简单的方法来在两个过程之间进行通信

第一个过程创建映射的内存,在其中写下第一个消息,并为另一个过程复制句柄(映射的内存未命名)代码是这样的:

hMapped = CreateFileMappingA(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 1000, "my_shared_memory");
if (!hMapped)
{
    cout << "[!] failed to create the shared mapped memory with error : " << GetLastError() << endl;
    getchar();
}
char *shared_buffer = (char*)MapViewOfFile(hMapped, FILE_MAP_ALL_ACCESS, 0, 0, mapped_memory_size);

然后,另一个过程获取手柄,打开视图并检索第一个缓冲区编写

然后循环并每6秒检查一次新的写作操作并处理

我从第一个过程中写下:

std::lock_guard<std::mutex> lock(mtx);
RtlSecureZeroMemory(shared_buffer, mapped_memory_size);
shared_buffer[0] = 'n'; // it's a hint for the other process
memcpy(shared_buffer + 1, this->stuff.get_data().c_str(), this->stuff.get_data().size() + 1);

,但在第二个过程中没有更新缓冲区,它是第一个缓冲区

这是第二个过程中的代码:

HANDLE shared_memory = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, "my_shared_memory");
char *shared_buffer = (char*)MapViewOfFile(shared_memory, FILE_MAP_ALL_ACCESS, 0, 0, 1000);
utils::command_line_parser cmd_parser;
cmd_parser.parse(std::string((char*)shared_buffer + 1));
if (!cmd_parser.valid()) { // I get that they are valid and I verify that
    printf("failed to parse the arguments !");
    return TRUE;
}
while(true)
{
    Sleep(6000);
    // CloseHandle(shared_memory);
    // shared_memory = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, "my_shared_memory");
    // shared_buffer = (char*)MapViewOfFile(shared_memory, FILE_MAP_ALL_ACCESS, 0, 0, 1000);
    MessageBoxA(0, (char*)shared_buffer, "message", 0);
    char res = shared_buffer[0];
    switch (res)
    {
    case 'q' :
        // do some stuff
    case 'n' :
        // do some stuff
        break;
    default:
        break;
    }

这是一个命名共享存储器应用程序的示例,该应用程序似乎与Visual Studio 2015一起使用。该应用程序可以作为作者作为记忆区域的作者或从内存区域中的读取器运行在指定的命令行参数上。

查看Microsoft的文档,看起来像是分享一个句柄需要流程分叉。createFileMappinga函数以及您的原始帖子和似乎不是您在做什么的问题。

多个进程可以通过使用一个来共享同一文件的视图 单个共享文件映射对象或创建单独的文件映射 由同一文件备份的对象。单个文件映射对象可以是 通过多个过程通过在过程中继承句柄共享 创建,重复手柄或打开文件映射对象 按名字。有关更多信息,请参阅createProcess,副本 和OpenFileMapping功能。

在下面的示例中使用命名共享存储区域,我从createFileMapping,mapViewOffile,whanding泄漏的c 的示例的源代码开始作为两个不同行为的不同过程。

用于以两种不同方式执行的源代码文件:

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#define BUF_SIZE 256
TCHAR szName[] = TEXT("MyFileMappingObject");
int main(int argc, char **argv)
{
    HANDLE hMapFile;
    LPCTSTR pBuf;
    int     iInstance = 0;
    TCHAR szMsgFmt[] = TEXT("Message from first process %d.");
    if (argc > 1) {
        iInstance = atoi(argv[1]);
    }
    hMapFile = CreateFileMapping(
        INVALID_HANDLE_VALUE,    // use paging file
        NULL,                    // default security
        PAGE_READWRITE,          // read/write access
        0,                       // maximum object size (high-order DWORD)
        BUF_SIZE,                // maximum object size (low-order DWORD)
        szName);                 // name of mapping object
    DWORD lastError = GetLastError();
    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not create file mapping object (%d).n"),
            GetLastError());
        std::cin.get();
        return 1;
    }
    pBuf = (LPTSTR)MapViewOfFile(hMapFile,   // handle to map object
        FILE_MAP_ALL_ACCESS, // read/write permission
        0,
        0,
        BUF_SIZE);
    if (pBuf == NULL)
    {
        _tprintf(TEXT("Could not map view of file (%d).n"),
            GetLastError());
        CloseHandle(hMapFile);
        std::cin.get();
        return 1;
    }

    for (int i = 1; i < 4; i++) {
        if (iInstance > 0) {
            MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
        }
        else {
            TCHAR szMsg[128] = { 0 };
            wsprintf (szMsg, szMsgFmt, i);
            std::cout << "Copying text into shared memory " << i << std::endl;
            CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
            std::cout << "Waiting " << std::endl;
            _getch();
        }
    }
    CloseHandle(hMapFile);

    UnmapViewOfFile(pBuf);
    return 0;
}

批处理文件1和批处理文件2以运行与两个不同过程相同的可执行文件。

shared_mem
pause

shared_mem  1
pause

和修改后的stdafx.h包括文件。

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <Windows.h>
#include <WinUser.h>
#include <stdio.h>
#include <tchar.h>

// TODO: reference additional headers your program requires here

编译应用程序源文件,然后运行第一个将启动的BAT文件,并将一些文本放入共享存储区域,然后等待。接下来运行第二个BAT文件,该文件将从共享内存区域读取文本。

我看到的是,如果您单击第二个蝙蝠文件中显示的对话框上的"确定"按钮,您将再次看到相同的消息。

但是,如果然后转到第一个BAT文件的窗口,然后按Enter键生成下一个消息,然后返回到第二个BAT文件生成的对话框,您将看到它将读取更新的字符串。

这全都使用了一个命名的共享文件。没有尝试共享内存句柄。