C++ 共享内存实验例程

服务端:

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

using namespace std;

#define MAP_PREFIX L"Local\\"
#define MAP_NAME   L"SampleMap"
#define FULL_MAP_NAME MAP_PREFIX MAP_NAME

#define  MAP_SIZE      65536

#define VIEW_OFFSIZE   0

#define VIEW_SIZE      1024

#define MESSAGE L"Message from the first process."

struct STUDENT
{
	int age;
	int sex;
	int classes;
	int parent;
};

typedef struct TEST 
{
	BOOL bOK;
	int num;
	char a[20];        //使用指针的时候在读取会出错
	STUDENT student[2];
}*LPTEST;


int main()
{
	HANDLE hMapFile = NULL;
	/*PVOID pView = NULL;*/

	LPTEST pTest = NULL;

	hMapFile = CreateFileMapping(
		INVALID_HANDLE_VALUE,
		NULL,
		PAGE_READWRITE,
		0,
		MAP_SIZE,
		FULL_MAP_NAME
	);
	if (hMapFile == NULL)
	{
		wprintf(L"CreateFileMapping failed");
		goto Cleanup;
	}
	wprintf(L"The file mapping(%s) is created\n", FULL_MAP_NAME);

	//pView = MapViewOfFile(
	//	hMapFile,
	//	FILE_MAP_ALL_ACCESS,
	//	0,
	//	VIEW_OFFSIZE,
	//	VIEW_SIZE
	//);
	//if (pView == NULL)
	//{
	//	wprintf(L"MapViewOfFile failed");
	//	goto Cleanup;
	//}
	pTest = (LPTEST)MapViewOfFile(
		hMapFile,
		FILE_MAP_ALL_ACCESS,
		0,
		VIEW_OFFSIZE,
		/*VIEW_SIZE*/
		sizeof(LPTEST)
	);
	if (pTest == NULL)
	{
		wprintf(L"MapViewOfFile failed");
		goto Cleanup;
	}
	wprintf(L"The file view is mapped\n");

	//PWSTR pszMessage = MESSAGE;
	//DWORD cbMessage = (wcslen(pszMessage) + 1) * sizeof(*pszMessage);
	//memcpy_s(pView, VIEW_SIZE, pszMessage, cbMessage);
	//wprintf(L"This message is written to the view:\n\"%s\"\n", pszMessage);
	pTest->bOK = TRUE;
	pTest->num = 3;
	char* p = "abecdfa";

	STUDENT* pStudent = new STUDENT[2];
	pStudent[0].age = 13;
	pStudent[0].classes = 3;
	pStudent[0].parent = 2;
	pStudent[0].sex = 0;

	pStudent[1].age = 12;
	pStudent[1].classes = 1;
	pStudent[1].parent = 2;
	pStudent[1].sex = 1;

	memcpy(pTest->student, pStudent,sizeof(STUDENT)*2);


	strcpy(pTest->a, p);
	cout << pTest->bOK << endl;
	cout << pTest->num << endl;
	cout << pTest->a << endl;
	cout << pTest->student[0].age << "," << pTest->student[0].classes << ","<< pTest->student[0].parent << ","<<pTest->student[0].sex << endl;
	cout << pTest->student[1].age << "," << pTest->student[1].classes << ","<< pTest->student[1].parent << ","<<pTest->student[1].sex << endl;
	wprintf(L"Press ENTER to clean up resources and quit");
	getchar();

Cleanup:
	if (hMapFile)
	{
		//if (pView)
		//{
		//	UnmapViewOfFile(pView);
		//	pView = NULL;
		//}
		if (pTest)
		{
			UnmapViewOfFile(pTest);
			pTest = NULL;
		}
		CloseHandle(hMapFile);
		hMapFile = NULL;
	}

	return 0;
}

客户端:

#include <stdio.h>
#include <windows.h>
#include <iostream>
using namespace std;


#define MAP_PREFIX L"Local\\"
#define MAP_NAME   L"SampleMap"
#define FULL_MAP_NAME MAP_PREFIX MAP_NAME

#define  VIEW_OFFSET    0
#define  VIEW_SIZE    1024

struct STUDENT
{
	int age;
	int sex;
	int classes;
	int parent;
};

typedef struct TEST
{
	BOOL bOK;
	int num;
	char a[20];
	STUDENT student[2];
}*LPTEST;

int main()
{
	HANDLE hMapFile = NULL;
	/*PVOID pView = NULL;*/
	LPTEST pTest = NULL;
	hMapFile = OpenFileMapping(
		FILE_MAP_READ | FILE_MAP_WRITE,
		FALSE,
		FULL_MAP_NAME
	);
	if (hMapFile == NULL)
	{
		wprintf(L"OpenFileMapping failed");
		goto Cleanup;
	}
	wprintf(L"The file mapping(%s) is opened\n", FULL_MAP_NAME);

	//pView = MapViewOfFile(
	//	hMapFile,
	//	FILE_MAP_READ,
	//	0,
	//	VIEW_OFFSET,
	//	VIEW_SIZE
	//);
	//if (pView == NULL)
	//{
	//	wprintf(L"MapViewOfFile failed");
	//	goto Cleanup;
	//}
	pTest = (LPTEST)MapViewOfFile(
		hMapFile,
		FILE_MAP_READ|FILE_MAP_WRITE,
		0,
		VIEW_OFFSET,
		/*VIEW_SIZE*/
		sizeof(LPTEST)
	);
	if (pTest == NULL)
	{
		wprintf(L"MapViewOfFile failed");
		goto Cleanup;
	}

	pTest->bOK = FALSE;

	wprintf(L"The file view is mapped\n");
	
	//wprintf(L"Read from the file mapping:\n\"%s\"\n", (PWSTR)pView);
	cout << pTest->bOK << endl;
	cout << pTest->num << endl;
	cout << pTest->a << endl;
	cout << pTest->student[0].age << "," << pTest->student[0].classes << "," << pTest->student[0].parent << "," << pTest->student[0].sex << endl;
	cout << pTest->student[1].age << "," << pTest->student[1].classes << "," << pTest->student[1].parent << "," << pTest->student[1].sex << endl;
	wprintf(L"Press ENTER to clean up resources and quit");
	getchar();
Cleanup:
	if (hMapFile)
	{
		//if (pView)
		//{
		//	UnmapViewOfFile(pView);
		//	pView = NULL;
		//}
		if (pTest)
		{
			UnmapViewOfFile(pTest);
			pTest = NULL;
		}
		CloseHandle(hMapFile);
		hMapFile = NULL;
	}
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值