win32共享内存简单实例

//win32ShareMemoryClient.cpp

#include <Windows.h> 
#include <iostream> 

using namespace std; 

int main()
{
	HANDLE hMutex      = NULL; 
	HANDLE hFileMapping   = NULL; 
	LPVOID lpShareMemory  = NULL;
	HANDLE hServerWriteOver = NULL; 
	HANDLE hClientReadOver = NULL; 

	hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "SM_Mutex"); 
	if (NULL == hMutex) 
	{
		if (ERROR_FILE_NOT_FOUND == GetLastError())
		{
			cout << "OpenMutex fail: file not found!" << endl;
		}
		else
		{
			cout << "OpenMutex fail:" << GetLastError() << endl; 
		}

		goto CLIENT_SHARE_MEMORY_END; 
	}

	//一旦互斥对象处于有信号状态,则该函数返回 
	if (WaitForSingleObject(hMutex, 5000) != WAIT_OBJECT_0)
	{
		cout << "WaitForSingle error:" << GetLastError() << endl; 
		goto CLIENT_SHARE_MEMORY_END;
	}

	//open share memory 
	hFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS,     
		FALSE, 
		"ShareMemoryTest"); 
	if (NULL == hFileMapping) 
	{
		cout << "OpenFileMapping" << GetLastError() << endl;
		goto CLIENT_SHARE_MEMORY_END; 
	}

	lpShareMemory = MapViewOfFile(hFileMapping,
		FILE_MAP_ALL_ACCESS, 
		0,
		0,
		0);
	if (NULL == lpShareMemory) 
	{
		cout << "MapViewOfFile " << GetLastError() << endl; 
		goto CLIENT_SHARE_MEMORY_END; 
	}

	//read and write data 
	hServerWriteOver = CreateEvent(NULL, 
		TRUE, 
		FALSE,
		"ServerWriteOver"); 

	hClientReadOver = CreateEvent(NULL, 
		TRUE, 
		FALSE,
		"ClientReadOver"); 

	if (NULL == hServerWriteOver || NULL == hClientReadOver)
	{
		cout << "CreateEvent " << GetLastError() << endl; 
		goto CLIENT_SHARE_MEMORY_END;
	}

	char p = 0; 
	char* q = (char*)lpShareMemory; 
	do 
	{
		if (!SetEvent(hClientReadOver))  
		{
			goto CLIENT_SHARE_MEMORY_END; 
		}

		if (WaitForSingleObject(hServerWriteOver, INFINITE) != WAIT_OBJECT_0)        
		{
			goto CLIENT_SHARE_MEMORY_END; 
		}

		p = q[0]; 
		putchar(p); 
		if (!ResetEvent(hServerWriteOver))  
		{
			goto CLIENT_SHARE_MEMORY_END; 
		}
	} while (p != '\n');

CLIENT_SHARE_MEMORY_END:
	if (NULL != hServerWriteOver)	CloseHandle(hServerWriteOver);   
	if (NULL != hClientReadOver)		CloseHandle(hClientReadOver);   
	if (NULL != lpShareMemory)		UnmapViewOfFile(lpShareMemory);   
	if (NULL != hFileMapping)			CloseHandle(hFileMapping);   
	if (NULL != hMutex)					ReleaseMutex(hMutex); 

	return 0;
}


//win32ShareMemoryServer.cpp

#include <Windows.h> 
#include <iostream> 

using namespace std; 

int main()
{
	HANDLE hMutex      = NULL; 
	HANDLE hFileMapping   = NULL; 
	LPVOID lpShareMemory  = NULL;   
	HANDLE hServerWriteOver = NULL;   
	HANDLE hClientReadOver = NULL; 

	//create share memory 
	hFileMapping = CreateFileMapping(	INVALID_HANDLE_VALUE,	//映射文件的句柄, 若设为0xFFFFFFFF(即:INVALID_HANDLE_VALUE)则创建一个进程间共享的对象
																NULL,									//安全属性
																PAGE_READWRITE,				//保护方式
																0,											//对象的大小
																1024*1024, 
																"ShareMemoryTest");			// 映射文件名,即共享内存的名称
	if (NULL == hFileMapping)
	{
		cout << "CreateFileMapping fail:" << GetLastError() << endl;
		goto SERVER_SHARE_MEMORY_END; 
	}

	lpShareMemory = MapViewOfFile(	hFileMapping, 
															FILE_MAP_ALL_ACCESS, 
															0,
															0,
															0);
	if (NULL == lpShareMemory)
	{
		cout << "MapViewOfFile" << GetLastError() << endl; 
		goto SERVER_SHARE_MEMORY_END; 
	}

	//创建互斥对象,初始时不占有该互斥量,互斥对象名字是"SM_Mutex"
	hMutex = CreateMutex(NULL, FALSE, "SM_Mutex"); 
	if (NULL == hMutex || ERROR_ALREADY_EXISTS == GetLastError())
	{
		cout << "CreateMutex" << GetLastError() << endl;
		goto SERVER_SHARE_MEMORY_END;
	}
	
	//事件对象就像一个开关:它只有两种状态---开和关。当一个事件处于”开”状态,我们称其为”有信号”否则称为”无信号”。
	//send data 
	hServerWriteOver = CreateEvent(NULL,		//采用默认属性
														 TRUE,		//创建的Event是自动复位还是人工复位.如果true,人工复位, 一旦该Event被设置为有信号,则它一直会等到ResetEvent()API被调用时才会恢复 为无信号.    
														 FALSE,		//初始状态,true有信号,false无信号   
														 "ServerWriteOver");	//事件对象的名称
	hClientReadOver = CreateEvent(NULL, 
													    TRUE,
														FALSE,
														"ClientReadOver"); 

	if (NULL == hServerWriteOver || NULL == hClientReadOver)
	{
		cout << "CreateEvent " << GetLastError() << endl; 
		goto SERVER_SHARE_MEMORY_END;
	}

	char p = 0; 
	char* q = (char*)lpShareMemory; 
	do 
	{
		p = getchar(); 
		//在事件对象生成后,必须调用WaitForSingleObject来让线程进入等待状态
		//当等待的时间超过该值后无信号同步对象仍处于”无信号”状态,线程不再等待, WaitForSingleObject函数会返回。
		//如果想要线程一直等待,请把该参数设为INFINITE(该值等于0xffffffff)。  
		if (WaitForSingleObject(hClientReadOver, 5*1000) != WAIT_OBJECT_0) 
		{
			goto SERVER_SHARE_MEMORY_END; 
		}

		q[0] = p; 
		if (!ResetEvent(hClientReadOver)) goto SERVER_SHARE_MEMORY_END;//把指定的事件对象设置为无信号状态     
		if (!SetEvent(hServerWriteOver)) goto SERVER_SHARE_MEMORY_END;//把指定的事件对象设置为有信号状态 
	} while (p != '\n');

SERVER_SHARE_MEMORY_END:
	//release share memory 
	if (NULL != hServerWriteOver)		CloseHandle(hServerWriteOver);   
	if (NULL != hClientReadOver)		CloseHandle(hClientReadOver);   
	if (NULL != lpShareMemory)		UnmapViewOfFile(lpShareMemory);   
	if (NULL != hFileMapping)			CloseHandle(hFileMapping);   
	if (NULL != hMutex)						ReleaseMutex(hMutex); 

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值