windows 共享内存

----------------------------------------父例程:创建内存块----------------------------------------------

ShareMemory.h

#pragma once

#include 
#include 

const DWORD ERROR_NOT_CREATE = 0x66001;

class CShareMemory
{
public:
	CShareMemory(void);
	virtual ~CShareMemory(void);

	DWORD Create(const TCHAR *pName, DWORD dwMaximumSizeLow,  
				 DWORD dwMaximumSizeHigh = 0, 
				 DWORD flProtect = PAGE_READWRITE, 
				 LPSECURITY_ATTRIBUTES lpAttributes = NULL);
	DWORD Write(byte *pbData, DWORD dwLength, 
				DWORD dwOffsetLow = 0, 
				DWORD dwOffsetHigh = 0);
	DWORD Read(byte *pbBuf, DWORD dwLength, 
			   DWORD dwOffsetLow = 0, 
			   DWORD dwFileOffsetHigh = 0);
	void  Close(void);
	DWORD SetAccess(DWORD dwDesiredAccess = FILE_MAP_ALL_ACCESS);
private:
	TCHAR *m_pName;
	HANDLE m_hShareMemory, m_hAccessMutex;
	BOOL m_bCreate;
	byte *m_pView;
};



ShareMemory.cpp

#include "ShareMemory.h"

CShareMemory::CShareMemory(void)
: m_pName(NULL), m_pView(NULL), m_hShareMemory(NULL), m_hAccessMutex(NULL), 
  m_bCreate(FALSE)
{
	m_pView = new byte;
}

CShareMemory::~CShareMemory(void)
{
	Close();
}

DWORD CShareMemory::Create(const TCHAR *pName, DWORD dwMaximumSizeLow, 
						   DWORD dwMaximumSizeHigh, DWORD flProtect, 
						   LPSECURITY_ATTRIBUTES lpAttributes)
{
	if (pName == NULL)
	{
		return ERROR_INVALID_PARAMETER;
	}
	
	m_pName = new TCHAR[_tcslen(pName)+1];
	_tcscpy(m_pName, pName);
	m_pName[_tcslen(pName)] = TEXT('\0');

	m_hShareMemory = CreateFileMapping(INVALID_HANDLE_VALUE, 
									   lpAttributes, 
									   flProtect, 
									   dwMaximumSizeHigh, 
									   dwMaximumSizeLow, 
									   m_pName);
	if (m_hShareMemory == NULL)
	{
		return GetLastError();
	}

	TCHAR *pMtx = new TCHAR[_tcslen(pName)+4];
	_tcscpy(pMtx, m_pName);
	pMtx[_tcslen(m_pName)] = TEXT('\0');
	_tcscat(pMtx, TEXT("Mtx"));
	pMtx[_tcslen(pName)+3] = TEXT('\0');
	m_hAccessMutex = CreateMutex(lpAttributes, FALSE, pMtx);
	if (m_hAccessMutex == NULL)
	{
		return GetLastError();
	}

	m_pView = (byte*)MapViewOfFile(m_hShareMemory, 
							       FILE_MAP_ALL_ACCESS, 
								   0, 
								   0, 
								   0);
	if (m_pView == NULL)
	{
		return GetLastError();
	}
	memset(m_pView, '\0', dwMaximumSizeLow);
	m_bCreate = TRUE;

	return 0;
}

DWORD CShareMemory::Write(byte *pbData, DWORD dwLength, 
						  DWORD dwOffsetLow,
						  DWORD dwOffsetHigh)
{
	DWORD dwErr(0);
	
	dwErr = WaitForSingleObject(m_hAccessMutex, INFINITE);
	if (dwErr != 0)
	{
		return dwErr;
	}

	memcpy(m_pView+dwOffsetLow, pbData, dwLength);
	ReleaseMutex(m_hAccessMutex);

	return 0;
}

DWORD CShareMemory::Read(byte *pbBuf, DWORD dwLength, 
						 DWORD dwOffsetLow, DWORD dwFileOffsetHigh)
{
	DWORD dwErr(0);

	dwErr = WaitForSingleObject(m_hAccessMutex, INFINITE);
	if (dwErr != 0)
	{
		return dwErr;
	}

	memcpy(pbBuf, m_pView+dwOffsetLow, dwLength);
	ReleaseMutex(m_hAccessMutex);

	return 0;
}

void CShareMemory::Close(void)
{
	if (m_bCreate)
	{
		delete [] m_pName;
		UnmapViewOfFile(m_pView);
		//delete m_pView;
		CloseHandle(m_hShareMemory);
		CloseHandle(m_hAccessMutex);

		m_bCreate = FALSE;
	}
}

DWORD CShareMemory::SetAccess(DWORD dwDesiredAccess)
{
	if (!m_bCreate)
	{
		return ERROR_NOT_CREATE;
	}
	DWORD dwErr(0);

	dwErr = WaitForSingleObject(m_hAccessMutex, INFINITE);
	if (dwErr != 0)
	{
		return dwErr;
	}
	UnmapViewOfFile(m_pView);
	m_pView = (byte*)MapViewOfFile(m_hShareMemory, 
	  							   FILE_MAP_ALL_ACCESS, 
								   0, 
								   0, 
								   0);
	ReleaseMutex(m_hAccessMutex);
	if (m_pView == NULL)
	{
		return GetLastError();
	}

	return 0;
}

main.cpp


#include "ShareMemory.h"
#include 

using namespace std;

void _tmain()
{
	cout<<"-----windows 共享内存父例程------------"<>pIn;
	dwErr = sm.Write((byte*)pIn, strlen(pIn));
	dwErr = sm.Read((byte*)pOut, 100);
	//char *p;// = (char*)pOut;
	cout<<(char*)pOut<>pIn;
	delete [] pIn;
	delete [] pOut;
}


----------------------------------------子例程:分享父例程创建的内存块----------------------------------------------



// shareMemery_test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 
#include 
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{


	cout<<"------windows 共享内存子例程--------------------"<



效果如下:


windows 共享内存_第1张图片

你可能感兴趣的:(OS(windows))