一则简单的Windows共享内存IPC代码

Windows共享内存可以让两个进程对同一块内存进行读写。

以下有2个进程,a.cpp生成A进程,b.cpp生成b进程。那么a进程将不停地读名为"Global\\MyFileMappingObject"的共享内存块,而b进程不停地写名为"Global\\MyFileMappingObject"的共享内存块。从而实现IPC。

//a.cpp
#include <windows.h>
#include <string.h>
#include <string>
#include <iostream>
#include <tchar.h>
using namespace std;

#define BUF_SIZE 256
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");    //指向同一块共享内存的名字

int main(int argc, char *argv[])
{
    HANDLE hMapFile;
    LPCTSTR pBuf;

    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

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not create file mapping object (%d).\n"),
            GetLastError());
        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);

        return 1;
    }
    //从main开始至此,A B process代码一样,都是获取名为"Global\\MyFileMappingObject"的共享内存的指针

    //以下代码,A不停地读共享内存pBuf
    while(1)
    {
        cout<<pBuf<<endl;
        cout<<"A process: hit keyboard to receive from B process"<<endl;
        getchar();
    }

    UnmapViewOfFile(pBuf);
    CloseHandle(hMapFile);
    return 0;
}

//b.cpp
#include <iostream>
#include <Windows.h>
#include <tchar.h>
using namespace std;

#define BUF_SIZE 256
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");    //指向同一块共享内存的名字

int main()
{
    HANDLE hMapFile;
    LPCTSTR pBuf;

    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

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not create file mapping object (%d).\n"),
            GetLastError());
        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);

        return 1;
    }
    //从main开始至此,A B process代码一样,都是获取名为"Global\\MyFileMappingObject"的共享内存的指针

    //以下代码,B不停写共享内存pBuf
    while(1)
    {
        TCHAR s[BUF_SIZE];
        cout<<"B process: plz input sth. to be transfered to A process."<<endl;
        cin>>s;
        memcpy((PVOID)pBuf, s, BUF_SIZE);
    }
}

======================另一个例子==========================

http://blog.csdn.net/zhao_fu_lu/article/details/41643539

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值