c++系统开发实例精粹内附的80例源代码 环境:windows2000,c++6.0

源代码在线查看: ipc.cpp

软件大小: 21089 K
上传用户: likefunv
关键词: windows 2000 6.0
下载地址: 免注册下载 普通下载 VIP

相关代码

				#include 
				#include 
				#include 
				
				#include "IPC.h"
				
				//***********************************************
				CIPC::CIPC() : m_hFileMap(NULL), m_hMutex(NULL)
				{
				}
				
				//***********************************************
				CIPC::~CIPC()
				{
					CloseIPCMMF();
					Unlock();
				}
				
				//***********************************************
				bool CIPC::CreateIPCMMF(void)
				{
					bool bCreated = false;
				
					try
					{
						if(m_hFileMap != NULL)
							return false;	// Already created
				
						// 创建4k的内存映射文件
						m_hFileMap = CreateFileMapping((HANDLE)0xFFFFFFFF,
							NULL,
							PAGE_READWRITE,
							0,
							4096,
							IPC_SHARED_MMF);
						if(m_hFileMap != NULL)
							bCreated = true;
					}
					catch(...) {}
				
					return bCreated;
				}
				
				//***********************************************
				bool CIPC::OpenIPCMMF(void)
				{
					bool bOpened = false;
				
					try
					{
						if(m_hFileMap != NULL)
							return true;	// Already opened
				        //打开内存映射文件
						m_hFileMap = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE,
							FALSE,
							IPC_SHARED_MMF);
						if(m_hFileMap != NULL)
							bOpened = true;
					}
					catch(...) {}
				
					return bOpened;
				}
				
				//***********************************************
				void CIPC::CloseIPCMMF(void)
				{
					try
					{
						if(m_hFileMap != NULL)
							CloseHandle(m_hFileMap), m_hFileMap = NULL;
					}
					catch(...) {}
				}
				
				//***********************************************
				bool CIPC::ReadIPCMMF(LPBYTE pBuf, DWORD &dwBufSize)
				{
					_ASSERTE(pBuf);
				
					bool bSuccess = true;
				
					try
					{
						if(m_hFileMap == NULL)
							return false;
				        //将内存映射文件映射到本进程地址空间
						DWORD dwBaseMMF = (DWORD)MapViewOfFile(m_hFileMap,
							FILE_MAP_READ | FILE_MAP_WRITE,
							0, 0, 0);
						_ASSERTE(dwBaseMMF);
				
						// 读取共享数据
						DWORD dwSizeofInBuf = dwBufSize;
						CopyMemory(&dwBufSize, (LPVOID)dwBaseMMF, sizeof(DWORD));
				
						if(dwSizeofInBuf != 0)
						{
							if(dwBufSize > dwSizeofInBuf)
								bSuccess = false;
							else
								CopyMemory(pBuf, (LPVOID)(dwBaseMMF + sizeof(DWORD)), dwBufSize);
						}
				        //解除本进程地址映射
						UnmapViewOfFile((LPVOID)dwBaseMMF);
					}
					catch(...) {}
				
					return bSuccess;
				}
				
				//***********************************************
				bool CIPC::WriteIPCMMF(const LPBYTE pBuf, const DWORD dwBufSize)
				{
					_ASSERTE(pBuf);
				
					bool bSuccess = true;
				
					try
					{
						if(m_hFileMap == NULL)
							return false;
				        //将内存映射文件映射到本进程地址空间
						DWORD dwBaseMMF = (DWORD)MapViewOfFile(m_hFileMap,
							FILE_MAP_READ | FILE_MAP_WRITE,
							0, 0, 0);
						_ASSERTE(dwBaseMMF);
				
						// 向共享区域写入数据
						CopyMemory((LPVOID)dwBaseMMF, &dwBufSize, sizeof(DWORD));
						CopyMemory((LPVOID)(dwBaseMMF + sizeof(DWORD)), pBuf, dwBufSize);
				
						UnmapViewOfFile((LPVOID)dwBaseMMF);
					}
					catch(...) {}
				
					return bSuccess;
				}
				
				//***********************************************
				bool CIPC::Lock(void)
				{
					bool bLocked = false;
				
					try
					{
						// 得到互斥体的句柄
						m_hMutex = CreateMutex(NULL, FALSE, IPC_MUTEX);
						if(m_hMutex != NULL)
						{
							// 等待锁住
							if(WaitForSingleObject(m_hMutex, INFINITE) == WAIT_OBJECT_0)
								bLocked = true;
						}
					}
					catch(...) {}
				
					return bLocked;
				}
				
				//***********************************************
				void CIPC::Unlock(void)
				{
					try
					{
						if(m_hMutex != NULL)
						{
							ReleaseMutex(m_hMutex); //解锁
							CloseHandle(m_hMutex);
							m_hMutex = NULL;
						}
					}
					catch(...) {}
				}
							

相关资源