home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / memory / nmmemsrv.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  3KB  |  116 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples.
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved.
  6. *       This source code is only intended as a supplement to
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12.  
  13. #include <windows.h>
  14. #include "memory.h"
  15.  
  16.  
  17. /**************************************************************************
  18.  * HANDLE CreateMapFile(char *MapFileName)
  19.  *
  20.  * Purpose: Create a Map file to map named share memory
  21.  *
  22.  * Inputs:  none
  23.  *
  24.  * Returns: MapFileHandle - a handle to the file
  25.  *                        or NULL if failure
  26.  *
  27.  * Calls:   CreateFile, ErrorOut
  28.  *
  29. \**************************************************************************/
  30. HANDLE CreateMapFile(char *MapFileName)
  31. {
  32.    HANDLE MapFileHandle;
  33.  
  34.    MapFileHandle= CreateFile(MapFileName,
  35.                              GENERIC_READ | GENERIC_WRITE,
  36.                              FILE_SHARE_READ | FILE_SHARE_WRITE,
  37.                              NULL,
  38.                              CREATE_ALWAYS,
  39.                              FILE_ATTRIBUTE_NORMAL,
  40.                              NULL);
  41.  
  42.    if (MapFileHandle == INVALID_HANDLE_VALUE)
  43.    {
  44.       ErrorOut("CreateFile");
  45.       return(NULL);
  46.    }
  47.    else
  48.       return(MapFileHandle);
  49.  
  50. }
  51.  
  52. /**************************************************************************
  53.  * HANDLE CreateMap(HANDLE FileToBeMapped, char *MapName )
  54.  *
  55.  * Purpose: Create File Mapping object using the open file handle
  56.  *
  57.  * Inputs:  FileToBeMapped - handle to the file
  58.  *
  59.  * Returns: MapHandle - handle to the file mapping object
  60.  *                    or NULL if failure
  61.  *
  62.  * Calls:   CreateFileMapping, ErrorOut
  63. \**************************************************************************/
  64.  
  65. HANDLE CreateMap(HANDLE FileToBeMapped, LPSTR MapName)
  66. {
  67.    HANDLE MapHandle;
  68.  
  69.    MapHandle= CreateFileMapping(FileToBeMapped,
  70.                                 NULL,
  71.                                 PAGE_READWRITE,
  72.                                 0,
  73.                                 4096,
  74.                                 MapName);
  75.  
  76.    if (MapHandle == NULL)
  77.    {
  78.       ErrorOut("CreateFileMapping");
  79.       return(NULL);
  80.    }
  81.    else
  82.       return(MapHandle);
  83. }
  84.  
  85.  
  86. /**************************************************************************
  87.  * LPVOID MapView(HANDLE hMap)
  88.  *
  89.  * Purpose: Map the file mapping object into address space
  90.  *
  91.  * Inputs:  hMap - handle to the mapping object
  92.  *
  93.  * Returns: MappedPointer - pointer to the address space that the
  94.  *                        object is mapped into
  95.  *                        or NULL if failure
  96.  *
  97.  * Calls:   MapViewOfFile, ErrorOut
  98.  *
  99. \**************************************************************************/
  100.  
  101. LPVOID MapView(HANDLE hMap)
  102. {
  103.    LPVOID MappedPointer;
  104.  
  105.    MappedPointer= MapViewOfFile(hMap,
  106.                                 FILE_MAP_WRITE | FILE_MAP_READ,
  107.                                 0, 0, 4096);
  108.    if (MappedPointer == NULL)
  109.    {
  110.       ErrorOut("MapViewOfFile");
  111.       return(NULL);
  112.    }
  113.    else
  114.       return(MappedPointer);
  115. }
  116.