Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1447451
  • 博文数量: 596
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 173
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-06 15:50
个人简介

在线笔记

文章分类

全部博文(596)

文章存档

2016年(1)

2015年(104)

2014年(228)

2013年(226)

2012年(26)

2011年(11)

分类: Windows平台

2014-01-14 14:05:47

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. }

阅读(4859) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~