共有メモリを使う

OSの差異はどうとでも吸収できそう。

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

#define SHM_SIZE 256
#define die(fmt, ...) \
  do { \
    fprintf(stderr, fmt, __VA_ARGS__); \
    exit(1); \
  } while(0)


int main() {
  HANDLE hShm;
  void *p;

  hShm = CreateFileMappingA((HANDLE) -1, NULL, PAGE_READWRITE, 0, SHM_SIZE, "foo");

  if (hShm == NULL) {
    die("CreateFileMappingA failed: %d\n", GetLastError());
  }

  p = MapViewOfFile(hShm, FILE_MAP_WRITE, 0, 0, 0);

  if (p == NULL) {
    die("MapViewOfFile failed: %d\n", GetLastError());
  }

  sprintf_s(p, SHM_SIZE, "Hello, Shared Memory.\n");
  printf(p);

  if (!UnmapViewOfFile(p)) {
    CloseHandle(hShm);
    die("UnmapViewOfFile failed: %d", GetLastError());
  }

  if (!CloseHandle(hShm)) {
    die("CloseHandle failed: %d", GetLastError());
  }

  return 0;
}