1. 必须先创建共享内存,才能够打开
2. 关闭共享内存后,就无法打开
3. 使用Global则必须是管理员权限才能创建或打开
相当于要读写一个文件,必须先创建文件才能够打开,删掉文件后就无法打开。权限不够的话就无法读写
阅读(2723) | 评论(0) | 转发(0) |
<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>
2. 关闭共享内存后,就无法打开
3. 使用Global则必须是管理员权限才能创建或打开
相当于要读写一个文件,必须先创建文件才能够打开,删掉文件后就无法打开。权限不够的话就无法读写
- // share_memory.cpp : 定义控制台应用程序的入口点。
- //
-
- #include "stdafx.h"
- #include <Windows.h>
- #include <stdio.h>
- #include <conio.h>
- #include <tchar.h>
-
- #define BUF_SIZE 256
- TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
- TCHAR szMsg[]=TEXT("Message from first process.");
-
- int p1();
- int p2();
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- if ( argc == 1 )
- {
- p1();
- }
- else
- {
- p2();
- }
- return 0;
- }
-
-
- int p1()
- {
- 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;
- }
-
-
- CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
- _getch();
-
- UnmapViewOfFile(pBuf);
-
- CloseHandle(hMapFile);
-
- return 0;
- }
-
- int p2()
- {
- HANDLE hMapFile;
- LPCTSTR pBuf;
-
- hMapFile = OpenFileMapping(
- FILE_MAP_ALL_ACCESS, // read/write access
- FALSE, // do not inherit the name
- szName); // name of mapping object
-
- if (hMapFile == NULL)
- {
- _tprintf(TEXT("Could not open 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;
- }
-
- MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
-
- UnmapViewOfFile(pBuf);
-
- CloseHandle(hMapFile);
-
- return 0;
- }
相关热门文章
给主人留下些什么吧!~~
评论热议