Back to index

lightning-sunbird  0.9+nobinonly
mapismem.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 //
00038 //  smem.cpp - This deals with all shared memory functions needed for 
00039 //  the MAPI component of Communicator
00040 //  Written by: Rich Pizzarro (rhp@netscape.com)
00041 //  November 1997
00042 //
00043 #include <windows.h>
00044 #include <windowsx.h>
00045 
00046 #include "mapismem.h"
00047 
00048 
00049 #ifndef ZeroMemory
00050 #include <memory.h>
00051 #define ZeroMemory(PTR, SIZE) memset(PTR, 0, SIZE)
00052 #endif // ZeroMemory
00053 
00054 // 
00055 // *create new* shared memory chunk 
00056 // once this is created, use the pointer
00057 // to the segment to to store data 
00058 // e.g.: 
00059 //     lpString = "string for communicator"; 
00060 //     lstrcpy((LPSTR)pData->m_buf[0], lpString); 
00061 //  
00062 CSharedMem *
00063 NSCreateSharedMemory(DWORD memSize, LPCTSTR memName, HANDLE *hSharedMemory)
00064 {
00065 #ifdef WIN32
00066 
00067   BOOL bExistedBefore;
00068   CSharedMem *pData;
00069 
00070   LPCTSTR szObjectName = memName; 
00071   DWORD dwSize = sizeof(CSharedMem) + memSize; 
00072   *hSharedMemory = CreateFileMapping( 
00073     (HANDLE)0xFFFFFFFF,0,PAGE_READWRITE,0,dwSize,szObjectName); 
00074   if(*hSharedMemory == 0) 
00075   { 
00076     return NULL; 
00077   } 
00078   bExistedBefore = (GetLastError() == ERROR_ALREADY_EXISTS); 
00079   if(bExistedBefore) 
00080   { 
00081        return NULL;
00082   } 
00083 
00084   pData = (CSharedMem *)MapViewOfFile( 
00085             *hSharedMemory, FILE_MAP_ALL_ACCESS, 0, 0, 0); 
00086   if(pData == NULL) 
00087   { 
00088     return NULL; 
00089   } 
00090 
00091   ZeroMemory(pData, dwSize); 
00092   pData->m_dwSize = memSize; 
00093 
00094   return pData;
00095 #else
00096   CSharedMem  *sMemChunk = NULL;
00097   DWORD dwSize = memSize = (sizeof(CSharedMem) + memSize); 
00098 
00099   if (sMemChunk != NULL)
00100          return(sMemChunk);
00101 
00102   sMemChunk = (CSharedMem *) GlobalAllocPtr(GMEM_MOVEABLE, dwSize);
00103   ZeroMemory(sMemChunk, (size_t) dwSize);
00104   sMemChunk->m_dwSize = dwSize;   // Missing in Communicator code!
00105   return(sMemChunk);
00106 
00107 #endif // WIN32
00108 }
00109 
00110 // 
00111 // *open existing* shared memory chunk 
00112 // once you have the pointer to the new segment 
00113 // use this pointer to access data, e.g.: 
00114 // 
00115 CSharedMem *
00116 NSOpenExistingSharedMemory(LPCTSTR memName, HANDLE *hSharedMemory)
00117 {
00118 #ifdef WIN32
00119   CSharedMem *pData;
00120   DWORD dwSize;
00121 
00122   LPCTSTR szObjectName = memName; 
00123   *hSharedMemory = OpenFileMapping( 
00124     FILE_MAP_WRITE,FALSE,szObjectName); 
00125   if(*hSharedMemory == 0) 
00126   { 
00127     return NULL; 
00128   } 
00129 
00130   pData = (CSharedMem *)MapViewOfFile( 
00131               *hSharedMemory,FILE_MAP_ALL_ACCESS,0,0,0); 
00132   if(pData == NULL) 
00133   { 
00134     return NULL; 
00135   } 
00136   
00137   dwSize = pData->m_dwSize; 
00138   return pData;
00139 #else
00140 
00141   return(NULL);   // In Win16, this is really meaningless...
00142 
00143 #endif
00144 }  
00145 
00146 // 
00147 // to close shared memory segment
00148 // 
00149 void
00150 NSCloseSharedMemory(CSharedMem *pData, HANDLE hSharedMemory)
00151 {
00152 #ifdef WIN32
00153   if(pData != 0) 
00154   { 
00155     UnmapViewOfFile(pData); 
00156     pData = 0; 
00157   } 
00158 
00159   if(hSharedMemory != 0) 
00160   { 
00161     CloseHandle(hSharedMemory); 
00162     hSharedMemory = 0; 
00163   } 
00164 #else
00165 
00166        if (pData != NULL)
00167        {
00168               GlobalFreePtr(pData);
00169               pData = NULL;
00170        }
00171 
00172 #endif // WIN32
00173 }