共享内存传递结构体类型的数据

一、firstprocess

//
#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 _tmain()
{
   HANDLE hMapFile;
   LPCTSTR pBuf;

   struct PBRecord
   {
	   //	bt_addr btAddr;
	   WCHAR wzName[32];
	   WCHAR wzNumber[64];
   };

   PBRecord record[3];
   for (int i=0;i<3;i++)
   {
	  ZeroMemory(record[i].wzName,2*sizeof(record[i].wzName));
	   ZeroMemory(record[i].wzNumber,2*sizeof(record[i].wzNumber));
   }
   //record.wzName = L"mamengmeng";
   wmemcpy(record[0].wzName,L"mamengmeng",wcslen(L"mamengmeng"));
  // record.wzNumber = L"13631592054";
   wmemcpy(record[0].wzNumber,L"13631592054",wcslen(L"13631592054"));

   wmemcpy(record[1].wzName,L"maoxian",wcslen(L"maoxian"));
   // record.wzNumber = L"13631592054";
   wmemcpy(record[1].wzNumber,L"15527245046",wcslen(L"15527245046"));

   wmemcpy(record[2].wzName,L"xiaoma",wcslen(L"xiaoma"));
   // record.wzNumber = L"13631592054";
   wmemcpy(record[2].wzNumber,L"10086",wcslen(L"10086"));


   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, &record, sizeof(record));
    _getch();

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

二、second process

//
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")


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


/* ds.nIndex = 1;
ds.szName = "马蒙蒙"; */
int _tmain()
{
   HANDLE hMapFile;
   LPCTSTR pBuf;
   struct PBRecord
   {
	   //	bt_addr btAddr;
	   WCHAR wzName[32];
	   WCHAR wzNumber[64];
   };


   PBRecord *record= new PBRecord[3];
   for (int i=0;i<3;i++)
   {
	   ZeroMemory(record[i].wzName,2*sizeof(record[i].wzName));
	   ZeroMemory(record[i].wzNumber,2*sizeof(record[i].wzNumber));
   }
   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;
   }


    record = (PBRecord*)MapViewOfFile(hMapFile, // handle to map object
               FILE_MAP_ALL_ACCESS,  // read/write permission
               0,
               0,
               BUF_SIZE);
   if (sizeof(record)== 0)
   {
      _tprintf(TEXT("Could not map view of file (%d).\n"),
             GetLastError());


      CloseHandle(hMapFile);


      return 1;
   }
   
   for(int i=0;i<3;i++)
   {
	  //  printf("point.x==%s--point.y==%s\r\n",record[i].wzName,record[i].wzNumber);
		// MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);


	   wprintf((_T("%s   "),record[i].wzName));
	   
	   wprintf((_T("Number %s\r\n"),record[i].wzNumber));
		wprintf(_T("\r\n"));
   }
	  getchar();
 
   //UnmapViewOfFile(pBuf);
   //CloseHandle(hMapFile);
   return 0;
}
//


致谢

1、共享内存传递结构体类型的数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值