sm_mz.zip

  • Gu Yongbin98
    了解作者
  • Visual C++
    开发工具
  • 10KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 1
    下载次数
  • 2014-05-29 23:12
    上传日期
共享内存实现,C++实现详细功能, 内容说明:Windows IPC
sm_mz.zip
  • SharedMemory.cpp
    55.6KB
  • Helpers.h
    692B
  • SharedMemory.h
    4.9KB
  • Helpers.cpp
    5.2KB
内容介绍
/******************************************************************************************** * MOD-NAME : SharedMemory.cpp * LONG-NAME : * * AUTHOR : Martin Ziacek * DEPARTMENT : * TELEPHONE : * CREATION-DATE : 09/05/2001 18:00:00 * SP-NO : * FUNCTION : * *********************************************************************************************/ #include "stdafx.h" #include "helpers.h" #include "SharedMemory.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CSharedMemory IMPLEMENT_DYNAMIC(CSharedMemory, CObject) /************************************************************************************** * PROG-NAME : * LONG-NAME : * FUNCTION : CSharedMemory::SetLastError * DESCRIPTION : * RETURN TYPE : void * ARGUMENT : DWORD dwErrCode * AUTHOR : Martin Ziacek * DATE OF LAST UPDATE : 13/11/2001 10:17:44 * GLOBAL READ : * GLOBAL WRITE : * FUNCTION CALLS : * ERROR-EXITS : * SIDE-EFFECTS : **************************************************************************************/ void CSharedMemory::SetLastError(DWORD dwErrCode) //this method and following macro will { //provide trace output of error description m_dwLastError = dwErrCode; //with file name and line number where ::SetLastError(dwErrCode); //error occures - it works correctly } //only in this order #ifdef _DEBUG #define SetLastError(err) DumpErrorCode(err,__LINE__,__FILE__); SetLastError(err) #endif /************************************************************************************** * PROG-NAME : * LONG-NAME : * FUNCTION : CSharedMemory::CSharedMemory * DESCRIPTION : * RETURN TYPE : * ARGUMENT : const TCHAR *szName * ARGUMENT : DWORD dwSize * ARGUMENT : PINITMEMORY InitMemoryProcedure * ARGUMENT : LPSECURITY_ATTRIBUTES lpsaAttributes * AUTHOR : Martin Ziacek * DATE OF LAST UPDATE : 13/11/2001 10:17:49 * GLOBAL READ : * GLOBAL WRITE : * FUNCTION CALLS : * ERROR-EXITS : * SIDE-EFFECTS : **************************************************************************************/ CSharedMemory::CSharedMemory(const TCHAR *szName, DWORD dwSize, PINITMEMORY InitMemoryProcedure,LPSECURITY_ATTRIBUTES lpsaAttributes ) : m_dwLastError(0), m_pNewAcl(NULL), m_pLogSid(NULL), m_pSysSid(NULL), m_pEvrSid(NULL), m_bCreated(FALSE), m_bFirst(TRUE), m_pSync(NULL), m_pLock(NULL), m_bSecPres(FALSE) { do { OSVERSIONINFO osvi; osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if (GetVersionEx(&osvi)) { TRACE(_T("osvi.dwMajorVersion = %d\n"),osvi.dwMajorVersion); TRACE(_T("osvi.dwMinorVersion = %d\n"),osvi.dwMinorVersion); if (osvi.dwMajorVersion >= 3) { if ((osvi.dwMinorVersion == 51) || (osvi.dwMinorVersion == 0) || (osvi.dwMinorVersion == 1)) { m_bSecPres = TRUE; } } } else { SetLastError(::GetLastError()); break; } //copy or create security descriptor for objects if (m_bSecPres) { if (lpsaAttributes) { if (!SetSa(lpsaAttributes)) { break; } } else { if (!CreateSa()) { break; } } } //create synchronization mechanism m_pSync = new CSemaphore(1,1,CString(szName)+CString(_T("_Sync")),m_bSecPres ? &m_SecAtr : NULL); if (!m_pSync) { break; } m_pLock = new CSingleLock(m_pSync); if (!m_pLock) { break; } m_pLock->Lock(); //lock access // atempt to create file mapping in th page file ... m_hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE,m_bSecPres ? &m_SecAtr : NULL,PAGE_READWRITE,0,dwSize,szName); // ... it can already exist (it is not error) ... if (::GetLastError() == ERROR_ALREADY_EXISTS) { m_bFirst = FALSE; //remember, we are not first } // ... it can fail ... if (m_hFileMap == NULL) { SetLastError(::GetLastError()); break; } // ... but if it did not, map a view m_lpView = (LPBYTE) MapViewOfFile(m_hFileMap,FILE_MAP_READ | FILE_MAP_WRITE,0,0,0); if (!m_lpView) { SetLastError(::GetLastError()); break; } //allow usage only of already existing size, could be less then requested if (m_bFirst) { ::ZeroMemory(m_lpView,dwSize); m_dwMemSize = dwSize; CopyMemory(m_lpView+sizeof(DWORD),&m_dwMemSize,sizeof(DWORD)); } else { CopyMemory(&m_dwMemSize,m_lpView+sizeof(DWORD),sizeof(DWORD)); } //if everything is OK up to now, the we have shared memory m_bCreated = TRUE; m_CsName = szName; //and it can be initialized in one place if (InitMemoryProcedure) { m_pLock->Unlock(); //temporary unlock to allow access from InitMemoryProcedure() InitMemoryProcedure(this); m_pLock->Lock(); } } while (FALSE); if (m_pLock) m_pLock->Unlock(); } /************************************************************************************** * PROG-NAME : * LONG-NAME : * FUNCTION : CSharedMemory::~CSharedMemory * DESCRIPTION : * RETURN TYPE : * AUTHOR : Martin Ziacek * DATE OF LAST UPDATE : 13/11/2001 10:17:55 * GLOBAL READ : * GLOBAL WRITE : * FUNCTION CALLS : * ERROR-EXITS : * SIDE-EFFECTS : **************************************************************************************/ CSharedMemory::~CSharedMemory() { if (m_lpView) UnmapViewOfFile(m_lpView); if (m_hFileMap) CloseHandle(m_hFileMap); if (m_pNewAcl) HeapFree(GetProcessHeap(), 0, m_pNewAcl); if (m_pLogSid) FreeSid(m_pLogSid); if (m_pSysSid) FreeSid(m_pSysSid); if (m_pEvrSid) FreeSid(m_pEvrSid); if (m_pLock) delete m_pLock; if (m_pSync) delete m_pSync; } /************************************************************************************** * PROG-NAME : * LONG-NAME : * FUNCTION : CSharedMemory::GetMemName * DESCRIPTION : * RETURN TYPE : CString * ARGUMENT : void * AUTHOR : Martin Ziacek * DATE OF LAST UPDATE : 13/11/2001 10:17:58 * GLOBAL READ : * GLOBAL WRITE : * FUNCTION CALLS : * ERROR-EXITS : * SIDE-EFFECTS : **************************************************************************************/ CString CSharedMemory::GetMemName(void) { if (m_bCreated) { return m_CsName; } else { SetLastError(ERROR_OBJECT_NOT_FOUND); return CString(_T("")); } } #ifdef _DEBUG /************************************************************************************** * PROG-NAME : * LONG-NAME : * FUNCTION : CSharedMemory::AssertValid * DESCRIPTION : * RETURN TYPE : void * AUTHOR : Martin Ziacek * DATE OF LAST UPDATE : 13/11/2001 10:18:02 * GLOBAL READ : * GLOBAL WRITE : * FUNCTION CALLS : * ERROR-EXITS : * SIDE-EFFECTS : **************************************************************************************/ void CSharedMemory::AssertValid() { CObject::AssertValid(); ASSERT(m_bCreated); ASSERT(m_hFileMap != NULL); ASSERT(m_hFileMap != INVALID_HANDLE_VALUE); ASSERT(m_lpView != NULL); ASSERT(m_pSync != NULL); ASSERT(m_pLock != NULL); } /************************************************************************************** * PROG-NAME : * LONG-NAME : * FUNCTION : C
评论
    相关推荐
    • X64 和X86VisualC++ 2017.zip
      这些组件是运行使用 Visual Studio 2017 开发的 C++ 应用程序所必需的,并与 Visual C+ 库动态链接。这些包可用于在计算机上运行此类应用程序,即使没有安装 Visual Studio 2017。这些包安装以下库的运行时组件:C ...
    • Visual C MFC编程实例
      Visual C MFC编程基础入门,实例讲解
    • visualc++6.0安装包
      visualc++6.0安装包。Microsoft Visual C++ 6.0是一款经典的学习C语言应用软件
    • vcredist:Visual C ++可再发行组件包
      Visual C ++可再发行组件包 Visual C ++可再发行组件包安装了运行组件,这些组件是运行使用Visual Studio生成的C ++应用程序所必需的。 描述 Visual C ++可再发行组件包在未安装Visual C ++的计算机上安装Visual C +...
    • VisualC14.0.zip
      “error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”这样的错误,可安装此程序即可继续pip install wordcloud
    • c语言 visual c++
      关于c语言的课件 大一课上使用的 个人觉得老师做的还不错
    • Visual studio助手
      用于辅助Visual studio的程序,希望能够帮助到大家,来源:C语言
    • MicrosoftVisualC2013运行库12.0.30501(3264)
      包含X64和x86版本, Visual C 可再发行组件包安装运行使用 Visual Studio 2013 预览版生成的 C 应用程序时所需的运行时组件,在未装有 Visual Studio 2013 的计算机上运行使用 Visual Studio 2013
    • VisualC#实例代码
      VisualC#实例源代码,VisualC#实例源代码,VisualC#实例源代码。
    • VisualC++6.0(二级C语言专用版)
      这个是绿色软件,计算机二级C语言专用版!只有7MB,删除了企业版的许多冗余内容,想练习的就下载用用吧