// ShareServer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <assert.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>

typedef struct
{
 int a;
 float b;
 double c;
 char buffer[64];

} MYSTRUCT;

int _tmain(int argc, _TCHAR* argv[])
{
 HANDLE hMemoryMap = NULL;
 LPBYTE pMemoryMap = NULL;

 hMemoryMap = ::OpenFileMapping(FILE_MAP_READ, FALSE, L"fork_server1");
 if(!hMemoryMap) {
  ::MessageBox(NULL, L"공유 메모리를 생성할 수 없습니다.", L"Error", MB_OK);
  return FALSE;
 }

 pMemoryMap = (BYTE*)::MapViewOfFile(hMemoryMap,FILE_MAP_READ,0,0,0);
 if(!pMemoryMap)
 {
  CloseHandle(hMemoryMap);
  ::MessageBox(NULL, L"공유 메모리를 열수 없습니다.",  L"Error", MB_OK);
  return FALSE;
 }

 char buffer[64];
 __int64 check = 0;
 MYSTRUCT* pStruct = (MYSTRUCT*)pMemoryMap;
 while(1)
 {
  int a = pStruct->a;
  float b = pStruct->b;
  double c = pStruct->c;
  strcpy(buffer, pStruct->buffer);

  printf("%s", pStruct->buffer);

  if(check++%100000 == 0)
  {
   if(_kbhit() != 0)
    break;
  }
 }

 if(pMemoryMap)
  UnmapViewOfFile(pMemoryMap);

 if(hMemoryMap)
  CloseHandle(hMemoryMap);
 return 0;
}

공유메모리 클라이언트 소스 이다.

+ Recent posts