#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <windows.h>
#include <string.h>
#include <winbase.h>


/*-----------------------------------------------------------------*/

void ErrorOut(char errstring[30])
/*
Purpose: Print out an meainful error code by means of
         GetLastError and printf

Inputs:  errstring - the action that failed, passed by the
                     calling proc.

Returns: none

Calls:   GetLastError
*/

{
DWORD Error;

Error= GetLastError();
printf("Error on %s = %d\n", errstring, Error);
}

/*-----------------------------------------------------------------*/
HANDLE OpenMap()
/*
Purpose: Open the mapping object pointed to by MapName

Inputs: none

Returns: handle to mapped object or NULL if failure

Calls: OpenFileMapping, ErrorOut
*/

{
HANDLE hAMap;
char MapName[30]="MapName1";

hAMap= OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE,
                       TRUE,
                       MapName);

if (hAMap == NULL)
  {
  ErrorOut("OpenFileMapping");
  return(NULL);
  }
else
  return(hAMap);

}

/*-----------------------------------------------------------------*/

LPVOID MapView(HANDLE *hMap)
/*
Purpose: Map the file mapping object into address space

Inputs:  *hMap - pointer to the mapping object

Returns: MappedPointer - pointer to the address space that the
                         object is mapped into
                         or NULL if failure

Calls:   MapViewOfFile, ErrorOut
*/

{
LPVOID MappedPointer;

MappedPointer= MapViewOfFile(*hMap,
                             FILE_MAP_WRITE | FILE_MAP_READ,
                             0,
                             0,
                             4096);
if (MappedPointer == NULL)
  {
  ErrorOut("MapViewOfFile");
  return(NULL);
  }
else
  return(MappedPointer);

}

/*-----------------------------------------------------------------*/

void main(void)
/*
Purpose: 1. Get a new console handle and create a window in the process
         2. Open the memory mapping object
         3. Map a view of the object
         4. print out the contents of the shared memory that was written
            by the server
         5. sleep before exiting and destroying window

Inputs:  none

Returns: none

Calls:   FreeConsole, AllocConsole, OpenMap, MapView, Sleep
*/

{
HANDLE hMem1;
LPVOID pShrMem1;
BOOL   FreeConSucc, AllocConSucc;

/* this console stuff allows a new window to be created,
   along with disassociating from the parent console and
   creating a new console for input/output */

FreeConSucc= FreeConsole();
if (FreeConSucc = FALSE)
  {
  ErrorOut("FreeConsole");
  ExitProcess(0);
  }
AllocConSucc= AllocConsole();
if (AllocConSucc = FALSE)
  {
  ErrorOut("AllocConsole");
  ExitProcess(0);
  }

printf ("Client: Named Shared Memory\n");

hMem1= OpenMap();
if (hMem1 == NULL)
  ExitProcess(0);

pShrMem1= MapView(&hMem1);
if (pShrMem1 == NULL)
  ExitProcess(0);

printf("Shared Memory Contents= %s\n", pShrMem1);

Sleep(5000);  /* wait before closing window */

}
