home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / mapismem.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.6 KB  |  155 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. //
  19. //  smem.cpp - This deals with all shared memory functions needed for 
  20. //  the MAPI component of Communicator
  21. //  Written by: Rich Pizzarro (rhp@netscape.com)
  22. //  November 1997
  23. //
  24. #include <windows.h>
  25. #include <windowsx.h>
  26.  
  27. #include "mapismem.h"
  28.  
  29.  
  30. #ifndef ZeroMemory
  31. #include <memory.h>
  32. #define ZeroMemory(PTR, SIZE) memset(PTR, 0, SIZE)
  33. #endif // ZeroMemory
  34.  
  35. // 
  36. // *create new* shared memory chunk 
  37. // once this is created, use the pointer
  38. // to the segment to to store data 
  39. // e.g.: 
  40. //     lpString = "string for communicator"; 
  41. //     lstrcpy((LPSTR)pData->m_buf[0], lpString); 
  42. //  
  43. CSharedMem *
  44. NSCreateSharedMemory(DWORD memSize, LPCTSTR memName, HANDLE *hSharedMemory)
  45. {
  46. #ifdef WIN32
  47.  
  48.   BOOL bExistedBefore;
  49.   CSharedMem *pData;
  50.  
  51.   LPCTSTR szObjectName = memName; 
  52.   DWORD dwSize = sizeof(CSharedMem) + memSize; 
  53.   *hSharedMemory = CreateFileMapping( 
  54.     (HANDLE)0xFFFFFFFF,0,PAGE_READWRITE,0,dwSize,szObjectName); 
  55.   if(*hSharedMemory == 0) 
  56.   { 
  57.     return NULL; 
  58.   } 
  59.   bExistedBefore = (GetLastError() == ERROR_ALREADY_EXISTS); 
  60.   if(bExistedBefore) 
  61.   { 
  62.       return NULL;
  63.   } 
  64.  
  65.   pData = (CSharedMem *)MapViewOfFile( 
  66.             *hSharedMemory, FILE_MAP_ALL_ACCESS, 0, 0, 0); 
  67.   if(pData == NULL) 
  68.   { 
  69.     return NULL; 
  70.   } 
  71.  
  72.   ZeroMemory(pData, dwSize); 
  73.   pData->m_dwSize = memSize; 
  74.  
  75.   return pData;
  76. #else
  77.   CSharedMem     *sMemChunk = NULL;
  78.   DWORD dwSize = memSize = (sizeof(CSharedMem) + memSize); 
  79.  
  80.   if (sMemChunk != NULL)
  81.       return(sMemChunk);
  82.  
  83.   sMemChunk = (CSharedMem *) GlobalAllocPtr(GMEM_MOVEABLE, dwSize);
  84.   ZeroMemory(sMemChunk, (size_t) dwSize);
  85.   sMemChunk->m_dwSize = dwSize;   // Missing in Communicator code!
  86.   return(sMemChunk);
  87.  
  88. #endif // WIN32
  89. }
  90.  
  91. // 
  92. // *open existing* shared memory chunk 
  93. // once you have the pointer to the new segment 
  94. // use this pointer to access data, e.g.: 
  95. // 
  96. CSharedMem *
  97. NSOpenExistingSharedMemory(LPCTSTR memName, HANDLE *hSharedMemory)
  98. {
  99. #ifdef WIN32
  100.   CSharedMem *pData;
  101.   DWORD dwSize;
  102.  
  103.   LPCTSTR szObjectName = memName; 
  104.   *hSharedMemory = OpenFileMapping( 
  105.     FILE_MAP_WRITE,FALSE,szObjectName); 
  106.   if(*hSharedMemory == 0) 
  107.   { 
  108.     return NULL; 
  109.   } 
  110.  
  111.   pData = (CSharedMem *)MapViewOfFile( 
  112.               *hSharedMemory,FILE_MAP_ALL_ACCESS,0,0,0); 
  113.   if(pData == NULL) 
  114.   { 
  115.     return NULL; 
  116.   } 
  117.   
  118.   dwSize = pData->m_dwSize; 
  119.   return pData;
  120. #else
  121.  
  122.   return(NULL);   // In Win16, this is really meaningless...
  123.  
  124. #endif
  125. }  
  126.  
  127. // 
  128. // to close shared memory segment
  129. // 
  130. void
  131. NSCloseSharedMemory(CSharedMem *pData, HANDLE hSharedMemory)
  132. {
  133. #ifdef WIN32
  134.   if(pData != 0) 
  135.   { 
  136.     UnmapViewOfFile(pData); 
  137.     pData = 0; 
  138.   } 
  139.  
  140.   if(hSharedMemory != 0) 
  141.   { 
  142.     CloseHandle(hSharedMemory); 
  143.     hSharedMemory = 0; 
  144.   } 
  145. #else
  146.  
  147.     if (pData != NULL)
  148.     {
  149.         GlobalFreePtr(pData);
  150.         pData = NULL;
  151.     }
  152.  
  153. #endif // WIN32
  154. }
  155.