Windows IPC 进程间通信之共享内存

来源:互联网 发布:lnmp 查看php日志 编辑:程序博客网 时间:2024/05/18 02:02
1. 必须先创建共享内存,才能够打开
2. 关闭共享内存后,就无法打开
3. 使用Global则必须是管理员权限才能创建或打开

相当于要读写一个文件,必须先创建文件才能够打开,删掉文件后就无法打开。权限不够的话就无法读写

  1. // share_memory.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include <Windows.h>
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <tchar.h>

  8. #define BUF_SIZE 256
  9. TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
  10. TCHAR szMsg[]=TEXT("Message from first process.");

  11. int p1();
  12. int p2();

  13. int _tmain(int argc, _TCHAR* argv[])
  14. {
  15.     if ( argc == 1 )
  16.     {
  17.         p1();
  18.     }
  19.     else
  20.     {
  21.         p2();
  22.     }
  23.     return 0;
  24. }


  25. int p1()
  26. {
  27.    HANDLE hMapFile;
  28.    LPCTSTR pBuf;

  29.    hMapFile = CreateFileMapping(
  30.                  INVALID_HANDLE_VALUE, // use paging file
  31.                  NULL, // default security
  32.                  PAGE_READWRITE, // read/write access
  33.                  0, // maximum object size (high-order DWORD)
  34.                  BUF_SIZE, // maximum object size (low-order DWORD)
  35.                  szName); // name of mapping object

  36.    if (hMapFile == NULL)
  37.    {
  38.       _tprintf(TEXT("Could not create file mapping object (%d).\n"),
  39.              GetLastError());
  40.       return 1;
  41.    }
  42.    pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
  43.                         FILE_MAP_ALL_ACCESS, // read/write permission
  44.                         0,
  45.                         0,
  46.                         BUF_SIZE);

  47.    if (pBuf == NULL)
  48.    {
  49.       _tprintf(TEXT("Could not map view of file (%d).\n"),
  50.              GetLastError());

  51.        CloseHandle(hMapFile);

  52.       return 1;
  53.    }


  54.    CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
  55.     _getch();

  56.    UnmapViewOfFile(pBuf);

  57.    CloseHandle(hMapFile);

  58.    return 0;
  59. }

  60. int p2()
  61. {
  62.    HANDLE hMapFile;
  63.    LPCTSTR pBuf;

  64.    hMapFile = OpenFileMapping(
  65.                    FILE_MAP_ALL_ACCESS, // read/write access
  66.                    FALSE, // do not inherit the name
  67.                    szName); // name of mapping object

  68.    if (hMapFile == NULL)
  69.    {
  70.       _tprintf(TEXT("Could not open file mapping object (%d).\n"),
  71.              GetLastError());
  72.       return 1;
  73.    }

  74.    pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
  75.                FILE_MAP_ALL_ACCESS, // read/write permission
  76.                0,
  77.                0,
  78.                BUF_SIZE);

  79.    if (pBuf == NULL)
  80.    {
  81.       _tprintf(TEXT("Could not map view of file (%d).\n"),
  82.              GetLastError());

  83.       CloseHandle(hMapFile);

  84.       return 1;
  85.    }

  86.    MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);

  87.    UnmapViewOfFile(pBuf);

  88.    CloseHandle(hMapFile);

  89.    return 0;
  90. }

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(2723) | 评论(0) | 转发(0) |
0

上一篇:用可变参数宏(variadic macros)传递可变参数表

下一篇:error C2440: “类型转换” : 无法从“overloaded-function”转换为

相关热门文章
  • LNK1123: 转换到 COFF 期间失...
  • WIN7访问共享:0x80070035 找不...
  • Delphi 2010下载+完美破解...
  • vs2010调试C++程序时提示 无...
  • VISIO,不规则封闭图形填充方...
  • linux dhcp peizhi roc
  • 关于Unix文件的软链接
  • 求教这个命令什么意思,我是新...
  • sed -e "/grep/d" 是什么意思...
  • 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~