Windows下FileMapping之进程通信

FileMapping之进程通信

读取端

#include <iostream>
#include <Windows.h>
#include <sddl.h>
#include <thread>
#include <chrono>
#pragma comment(lib, "Advapi32.lib")

char* pData = nullptr;
int main()
{
    // create event handle
    SECURITY_ATTRIBUTES sa;
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa.lpSecurityDescriptor = NULL;
    sa.bInheritHandle = TRUE;
    // common user support
    PSECURITY_DESCRIPTOR pSD = NULL;
    if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
        L"D:(A;;GA;;;AU)(A;;GA;;;SY)",
        1,//SDDL_REVISION_1,
        &pSD,
        NULL)) {
    }
    sa.lpSecurityDescriptor = pSD;
    HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 0x100, L"FileMappingTest");
    if (hMapping != NULL)
    {
        pData = (char*)MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
    }
    else {
        int nCode = GetLastError();
        std::cout << "Create file mapping failed: " << GetLastError() << "\n";
        return -1;
    }
    int nIndex = 0;
    while (1) {
        std::this_thread::sleep_for(std::chrono::milliseconds(3000));
        if (strlen(pData)) {
            std::cout << "Data: " << pData << "\n";
        }
        if ((++nIndex) >= 20)break;
    }
    getchar();
    UnmapViewOfFile(pData);
    CloseHandle(hMapping);
    return 0;
}

2、写入端

#include <iostream>
#include <Windows.h>
#include <sddl.h> 


#include <thread>
#include <chrono>
#pragma comment(lib, "Advapi32.lib")


char* pData = nullptr;
int main()
{
    const char* sharessdl = "D:P(A;OICI;GA;;;SY)(A;OICI;GA;;;BA)(A;OICI;GR;;;IU)";
    SECURITY_ATTRIBUTES security;
    ZeroMemory(&security, sizeof(security));
    security.nLength = sizeof(security);
    ConvertStringSecurityDescriptorToSecurityDescriptorA(
        sharessdl,
        SDDL_REVISION_1,
        &security.lpSecurityDescriptor,
        NULL);
    HANDLE hMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, L"FileMappingTest");
    if (hMapping != NULL)
    {
        pData = (char*)MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
#if 1
#if 0
        const char* pSrc = "exit";
#else
        DWORD nPid = GetCurrentProcessId();
        std::string strPid = std::to_string(nPid);
        const char* pSrc = strPid.c_str();
        //const char* pSrc = "34600";
#endif
        strcpy_s(pData, strlen(pSrc) + 1, pSrc);
        UnmapViewOfFile(pData);
        std::cout << "Set share data: " << strPid << "\n";
#endif
        HANDLE hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, L"EventFileMapping");
        if (NULL != hEvent) {
            SetEvent(hEvent);
            CloseHandle(hEvent);
            std::cout << "Notify event\n";
        }
        else {
            std::cout << "Open event error: " << GetLastError();
        }
    }
    else {
        int nCode = GetLastError();
        std::cout << "Create file mapping failed: " << GetLastError() << "\n";
        return -1;
    }

    getchar();
    CloseHandle(hMapping);
    return 0;
}

注意事项

1、32和64位程序创建的FileMapping可直接访问,没有位数限制
2、FileMapping可通过mutex和event进行同步
3、如果是服务,Mapping名字需要加Global\前缀(Event也一样),才能全局访问

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows进程通信是指在Windows操作系统中,不同进程之间进行数据交换、共享资源和协调操作的机制。Windows提供了多种进程通信的方式,常用的包括以下几种: 1. 管道(Pipe):管道是一种单向通信机制,可以在父子进程或者兄弟进程之间进行通信。Windows提供了两种类型的管道:匿名管道和命名管道。 2. 共享内存(Shared Memory):共享内存是一种高效的进程通信方式,它允许不同进程之间共享同一块内存区域。通过在进程之间映射同一块共享内存,进程可以直接读写该内存区域的数据。 3. 消息队列(Message Queue):消息队列是一种异步通信机制,进程可以将消息发送到队列中,其他进程可以从队列中读取消息。Windows提供了多种消息队列机制,包括邮件槽(MailSlot)、Windows消息队列等。 4. 套接字(Socket):套接字是一种网络通信机制,可以在不同计算机上的进程之间进行通信。Windows提供了基于TCP/IP协议的套接字接口,通过网络进行进程间通信。 5. 文件映射(File Mapping):文件映射是一种将文件映射到进程的内存空间的方式,不同进程可以通过访问同一块映射内存来实现通信。 除了上述方式,Windows还提供了其他进程通信的机制,如命名信号量、事件、互斥体等。开发人员可以根据具体需求选择适合的进程通信方式来实现进程间的数据交换和协作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值