Windows进程通信-共享内存

服务端

#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;
	char p = 0;
	char* q = NULL;

	//create share memory 
	hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE,
		NULL,
		PAGE_READWRITE,
		0,
		1024 * 1024,
		L"ShareMemoryTest");
	if (NULL == hFileMapping)
	{
		cout << "CreateFileMapping fail:" << GetLastError() << endl;
		goto SERVER_SHARE_MEMORY_END;
	}

	lpShareMemory = MapViewOfFile(hFileMapping,
		FILE_MAP_ALL_ACCESS,
		0,
		0,   //memory start address 
		0);   //all memory space 
	if (NULL == lpShareMemory)
	{
		cout << "MapViewOfFile" << GetLastError() << endl;
		goto SERVER_SHARE_MEMORY_END;
	}
	q = (char*)lpShareMemory;
	hMutex = CreateMutex(NULL, FALSE, L"SM_Mutex");
	if (NULL == hMutex || ERROR_ALREADY_EXISTS == GetLastError())
	{
		cout << "CreateMutex" << GetLastError() << endl;
		goto SERVER_SHARE_MEMORY_END;
	}//多个线程互斥访问 

	//send data 
	hServerWriteOver = CreateEvent(NULL,
		FALSE,
		FALSE,
		L"ServerWriteOver");
	hClientReadOver = CreateEvent(NULL,
		FALSE,
		TRUE,
		L"ClientReadOver");
	if (NULL == hServerWriteOver ||
		NULL == hClientReadOver)
{
		cout << "CreateEvent" << GetLastError() << endl;
		goto SERVER_SHARE_MEMORY_END;
	}


	do
	{
		if (WaitForSingleObject(hClientReadOver, 5 * 1000) != WAIT_OBJECT_0) goto SERVER_SHARE_MEMORY_END;
		q[0] = ++p;
		Sleep(500);
		if (!SetEvent(hServerWriteOver)) goto SERVER_SHARE_MEMORY_END;//把指定的事件对象设置为有信号状态 
	} while (p != 10);

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;
}

客户端

#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;
	char p = 0;
	char* q = NULL;

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

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

	//open share memory 
	hFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS,
		FALSE,
		L"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;
	}
	q = (char*)lpShareMemory;
	//read and write data 
	hServerWriteOver = CreateEvent(NULL,
		FALSE,
		FALSE,
		L"ServerWriteOver");
	hClientReadOver = CreateEvent(NULL,
		FALSE,
		TRUE,
		L"ClientReadOver");
	if (NULL == hServerWriteOver ||
		NULL == hClientReadOver)
	{
		cout << "CreateEvent" << GetLastError() << endl;
		goto CLIENT_SHARE_MEMORY_END;
	}


	do
	{
		if (WaitForSingleObject(hServerWriteOver, INFINITE) != WAIT_OBJECT_0) goto CLIENT_SHARE_MEMORY_END;
		p = q[0];
		printf("%d\n",p);
		if (!SetEvent(hClientReadOver)) goto CLIENT_SHARE_MEMORY_END;
	} while (p != 10);

CLIENT_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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值