A container class that operates to share memory! A variety of data structures can be used! But there is no part of the communication between the processes! It is convenient to expand!
^ _ ^
class CShareMem {public: CShareMem (LPCTSTR lpcszMemName = _T ( "ShareMemSection")); CShareMem (int cCount, size_t cSize, LPCTSTR lpcszMemName = _T ( "ShareMemSection")); virtual ~ CShareMem (); BOOL InitializeMem (int cCount, size_t cSize); size_t getDataSize () {return m_cDataSize;}; int GetDataCount () {return m_cDataCount;}; BOOL Clear (); BOOL Lock (DWORD dwMilliSeconds = INFINITE); BOOL Unlock (); LPVOID GetData (int nIndex = 0) ; BOOL GetData (int nIndex, LPVOID lpvData, size_t cSize); BOOL AddData (LPVOID lpvData, size_t cSize); BOOL SetData (int nIndex, LPVOID lpvData, size_t cSize); BOOL Delete (int nIndex); protected: LPVOID m_lpvData; / / Data pointer size_t m_cdataasize; // data element size (Byte) int m_cdataracount; // data element number BOOL m_finit; // Whether to initialize successful handle m_hmutant; // Mutex Handle M_HSharedMemoryFile; // Share memory core file handle CString M_STRM UtantName; // Mutual exclusion name cstring m_strsharememname; // Shared memory area name BOOL m_FEXIST; / / There is already};
#endif //! defined // ShareMem.cpp: Implementation of the csharemem class.////
#include "stdafx.h" #include "sharemem.h"
#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif
//// construction / destruction //
CShareMem :: CShareMem (LPCTSTR lpcszMemName) {m_cDataCount = 0; m_cDataSize = 0; m_fInit = FALSE; m_fExist = FALSE; m_lpvData = NULL; m_hSharedMemoryFile = NULL; // m_strMutantName = L "ShareMemMutant"; // m_strShareMemName = L "ShareMemSection" ; m_strShareMemName = lpcszMemName; m_strMutantName = lpcszMemName _T ( "Mutant");} CShareMem :: CShareMem (int cCount, size_t cSize, LPCTSTR lpcszMemName) {m_cDataCount = cCount; m_cDataSize = cSize; m_fInit = FALSE; m_lpvData = NULL; m_hSharedMemoryFile = NULL; m_fExist = FALSE; // m_strMutantName = L "ShareMemMutant"; // m_strShareMemName = L "ShareMemSection"; m_strShareMemName = lpcszMemName; m_strMutantName = lpcszMemName _T ( "Mutant");} CShareMem :: ~ CShareMem () {if ( m_finit) // {UNMAPVIEWOFFILE (m_lpvdata); CloseHandle (m_hsharedmemoryfile);}} /// // Bool Csharemem :: CLEAR () {MEMSET ((Pbyte) M_LPVData, 0, M_CDataSize * m_cdataacount); Return true;} catch (...) {returnaf false;}}
/// / NINDEX position in Nindex location // lpvoid csharemem :: getData (int NINDEX) {if (m_finit) {if (Nindex <0 || NINDEX> = m_cdataacount) Return Null; return (pbyte) m_lpvdata NINDEX * m_cdataasize;} else {return null;}} //// Nindex location data // bool csharemem :: getData (int NINDEX, LPVOID LPVDATA, SIZE_T CSIZE) {if (m_finit) {IF (NINDEX <0 | | nIndex> = m_cDataCount) return FALSE; if (! cSize = m_cDataSize) {return FALSE;} memcpy (lpvData, (PBYTE) m_lpvData nIndex * m_cDataSize, cSize); return TRUE;} else {return FALSE;}} // / / Delete Nindex Location Data // Bool CshareMem :: Delete (INT NINDEX) {if (Nindex <0 || NINDEX> = m_cdataracount) Return False; int NLEFT = m_cdataracount - NINDEX - 1; PBYTE PBBUFF = New Byte [NLEFT * M_CDATASIZE]; // Remove Head IF (0 == NINDEX) {MEMCPY (PBBUFF, (PBYTE) M_LPVData M_CDataS Ize, (m_cdataracount -1) * m_cdataasize); MEMSET (M_LPVData, 0, M_CDataCount * m_cdatazize); Memcpy (M_LPVData, PBBUFF, (M_CDataCount -1) * m_cdataasize);} // Remove else IF ((m_cdataacount -1) == nIndex) {memset ((PBYTE) m_lpvData (nIndex * m_cDataSize), 0, m_cDataSize);} else {memcpy (pbBuff, (PBYTE) m_lpvData (nIndex 1) * m_cDataSize, nLeft * m_cDataSize); memset ( (PBYTE) m_lpvData (nIndex) * m_cDataSize, 0, (m_cDataCount - nIndex) * m_cDataSize); memcpy ((PBYTE) m_lpvData (nIndex) * m_cDataSize, pbBuff, nLeft * m_cDataSize);} delete [] pbBuff;
M_CDataCount -; Return True;} else {return false;}} /// / Add data to data tail // BOOL CSHAREMEM :: AddData (lpvoid lpvdata, size_t csize) {{// IF ((m_cdata asse! = 0 && m_cdatasize! = csize)) {Return False;} if (! m_finit) {return false;} // // if (m_cdataracount == 0) Memcpy ((pbyte) m_lpvdata (m_cdataacount) * m_cdata asseze, lpvdata, csize M_CDataCount ; return true;} catch (cMemoryException * meme) {meme-> reporterror (); meme-> delete (); return false;} catch (...) {return false;}} //// Set data on NINDEX location // Bool Csharemem :: setData (int NINDEX, LPVOID LPVDATA, SIZE_T CSIZE) {if (m_finit) {if (Nindex <0 || NINDEX> = M_CDataCount) Return False; if (M_CDataSize! = CSIZE ) {Trace ("Data size does not match !!! / n")); return false;} MEMSET ((pbyte) m_lpvdata NINDEX * m_cdataasize, 0, csize); Memcpy ((Pbyte) M_LPVData NINDEX * M_CDATASIZE, LPVDATA, CSIZE); RETURN TRUE;} Else {Return False;}} /// // BOOL CSHAREMEM: : InitializeMem (int cCount, size_t cSize) {m_hMutant = CreateMutex (NULL, FALSE, m_strMutantName); m_cDataSize = cSize; m_cDataCount = cCount; // Create a memory-mapped files, shared memory region is set m_hSharedMemoryFile = :: CreateFileMapping ((HANDLE 0xfffffffff, // file handle NULL, // security attribute (WIN95 ignore) Page_readWrite, // File Protection Property 0, // Using Page File 1 <<