关于内存映射

//建立第一个工程

#include <windows.h>
#include <stdio.h>
#include <conio.h>

#define BUF_SIZE 256
TCHAR szName[]=TEXT("MyFileMappingObject");
TCHAR szMsg[]=TEXT("Message from first process");
int   szInt = 999;

void main()
{
   HANDLE hMapFile;
   LPCTSTR pBuf;

   hMapFile = CreateFileMapping(
                 INVALID_HANDLE_VALUE,    // use paging file
                 NULL,                    // default security
                 PAGE_READWRITE,          // read/write access
                 0,                       // max. object size
                 BUF_SIZE,                // buffer size 
                 szName);                 // name of mapping object
 
   if (hMapFile == NULL || hMapFile == INVALID_HANDLE_VALUE)
   {
      printf("Could not create file mapping object (%d)./n", GetLastError());
      return;
   }
   pBuf = (LPTSTR) MapViewOfFile(hMapFile,   // handle to mapping object
                        FILE_MAP_ALL_ACCESS, // read/write permission
                        0,                  
                        0,                  
                        BUF_SIZE);          
 
   if (pBuf == NULL)
   {
      printf("Could not map view of file (%d)./n", GetLastError());
      return;
   }

  
   CopyMemory((PVOID)pBuf, szMsg, strlen(szMsg));
   getch();

   UnmapViewOfFile(pBuf);

   CloseHandle(hMapFile);

//建立第二个工程

#include <windows.h>
#include <stdio.h>
#include <conio.h>

#define BUF_SIZE 256
TCHAR szName[]=TEXT("MyFileMappingObject");

void main()
{
   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)
   {
      printf("Could not open file mapping object (%d)./n", GetLastError());
      return;
   }
 
   pBuf = (LPTSTR) MapViewOfFile(hMapFile,    // handle to mapping object
               FILE_MAP_ALL_ACCESS,  // read/write permission
               0,                   
               0,                   
               BUF_SIZE);                  
 
   if (pBuf == NULL)
   {
      printf("Could not map view of file (%d)./n", GetLastError());
      return;
   }
  
   MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);

   UnmapViewOfFile(pBuf);

   CloseHandle(hMapFile);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值