VC 实现共享内存

#ifndef _CSHAREMEMORY_H_
#define _CSHAREMEMORY_H_

/*
 *   CopyRight (C) by dragonwarrior95@163.com. All rights is reserved.
 *   Time:     2013.04.09
 *   File:     CShareMemory.h
 *   Function: This file is written to finish the memory shared.
 *   Warning:  If you want to use this class, you must init one object then to call back InitMemory()
 */
#include <windows.h>

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

#define  BUF_SIZE  1024

class CShareMemory
{
public:
	LPVOID  m_lpMemory;// to receive the value that MapViewOfFile return

public:
	CShareMemory(LPCTSTR lpName);
	~CShareMemory();

	bool    InitMemory();
	LPVOID  ReadMemory();
	bool    ClearMemory();
	void    WriteMemory(LPVOID lpVoid);

private:	
	LPCTSTR m_lpName;  // name of mapping object
	HANDLE  m_hMapFile;// handle to map object

protected:
};
#endif  /*_CSHAREMEMORY_H_*/
#include "CShareMemory.h"
#include <sstream>

CShareMemory::CShareMemory(LPCTSTR lpName)
{
	if (NULL != lpName)
		m_lpName = lpName;
	else
		m_lpName = NULL;
	m_lpMemory = new char[BUF_SIZE];
	m_hMapFile = NULL;
}

CShareMemory::~CShareMemory()
{
	if (NULL != m_lpName)
		m_lpName = NULL;

	if (m_lpMemory != NULL) {
		UnmapViewOfFile(m_lpMemory);
		m_lpMemory = NULL;
	}
	
	if (m_hMapFile != NULL) {
		CloseHandle(m_hMapFile);
		m_hMapFile = NULL;
	}
}

bool CShareMemory::InitMemory()
{	
	if (m_lpName == NULL) {
		MessageBox(NULL, "m_lpName is NULL...", "Warning", 0);
		return false;
	}
	m_hMapFile = CreateFileMapping(
		INVALID_HANDLE_VALUE,    // use paging file
		NULL,                    // default security 
		PAGE_READWRITE,          // read/write access
		0,                       // max. object size 
		BUF_SIZE+1,              // buffer size  
		m_lpName);               // name of mapping object
	
	if (m_hMapFile == NULL) 
	{ 
		printf("Could not create file mapping object (%d).\n", 
			GetLastError());
		return false;
	}

	m_lpMemory = MapViewOfFile(m_hMapFile,   // handle to map object
		FILE_MAP_ALL_ACCESS,                 // read/write permission
		0,                   
		0,                   
		BUF_SIZE);           
	
	if (m_lpMemory == NULL) 
	{ 
		printf("Could not map view of file (%d).\n", 
			GetLastError()); 
		return false;
	}

	return true;
}

LPVOID CShareMemory::ReadMemory()
{
    return m_lpMemory;
}

bool CShareMemory::ClearMemory()
{
	if (m_lpMemory != NULL)
		delete [] m_lpMemory;
	else
		return false;

	return true;
}

void CShareMemory::WriteMemory(LPVOID lpVoid)
{
	if (NULL == lpVoid)
		m_lpMemory = NULL;
	else
		CopyMemory((char *)m_lpMemory, (char *)lpVoid, strlen((char *)lpVoid));
}

DWORD WINAPI ThreadProc1(LPVOID lpVoid);
DWORD WINAPI ThreadProc2(LPVOID lpVoid);

int main()
{
	HANDLE handle1 = CreateThread(NULL, 0, ThreadProc1, NULL, NULL, NULL);
	HANDLE handle2 = CreateThread(NULL, 0, ThreadProc2, NULL, NULL, NULL);

	CloseHandle(handle1);
	CloseHandle(handle2);
	getchar();

	return 0;
}

DWORD WINAPI ThreadProc1(LPVOID lpVoid)
{
	char buffer[128] = {0};
	CShareMemory *client = new CShareMemory("SHAREMEMORY");
	client->InitMemory();
	int count=0;
	while(1) {
		//if (client->ReadMemory() != NULL)
		//	continue;
		stringstream str;	
		count++;
		str<<count;		
		strcpy(buffer,"thread1 write 我是写入的数据");		
		strcat(buffer,str.str().c_str());
		client->WriteMemory((LPVOID)buffer);	
		cout<<buffer<<endl;
		Sleep(1500);
	}
	delete client;

	return 0;
}

DWORD WINAPI ThreadProc2(LPVOID lpVoid)
{
	char buffer[128] = {0};
	CShareMemory *server = new CShareMemory("SHAREMEMORY");
	server->InitMemory();
	
	while(1) {
		strcpy(buffer, (char *)server->ReadMemory());
		if (NULL != buffer) {
			cout<<"thread2 read:读物 "<<buffer<<endl;
		}
		Sleep(1000);
	}	
	delete server;

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值