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

问题描述 投票:-3回答:1

我在Windows上使用c ++并在两个进程之间进行通信的简单方法

第一个进程创建映射内存,将第一个消息写入其中并复制另一个进程的句柄(映射内存未命名)代码如下:

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;
    }
c++ winapi ipc memory-mapped-files
1个回答
1
投票

下面是一个名为共享内存应用程序的示例,该应用程序似乎与Visual Studio 2015一起使用。应用程序可以作为内存区域的写入程序或内存区域的读取程序运行,具体取决于指定的命令行参数。

查看Microsoft的文档,看起来共享句柄需要进程分叉。 CreateFileMappingA function和您原来的帖子和问题似乎不是您正在做的事情。

多个进程可以通过使用单个共享文件映射对象或创建由同一文件支持的单独文件映射对象来共享同一文件的视图。单个文件映射对象可以由多个进程共享,方法是在创建进程时继承句柄,复制句柄或按名称打开文件映射对象。有关更多信息,请参阅CreateProcess,DuplicateHandle和OpenFileMapping函数。

在下面使用命名共享内存区域的示例中,我从CreateFileMapping, MapViewOfFile, handle leaking c++中的示例的源代码开始,但是我只是做了一个单独的源文件,Windows控制台应用程序,我可以启动它作为具有不同行为的两个不同进程。

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

#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文件中单击显示的对话框上的Ok按钮,您将再次看到相同的消息。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.