title: Writing to shared memory
body: <p>How can I write from a file to shared memory using the Win32 API?</p>
  
  <p>I have this code:</p>
  
  <pre><code>hFile = CreateFile("input.map",
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ,
    NULL,
    OPEN_ALWAYS,
    FILE_ATTRIBUTE_NORMAL,
    NULL);
  
    hMapFile = CreateFileMapping(hFile,
    NULL,
    PAGE_READWRITE,
    0,
    0,
    TEXT("SharedObject"));
  
    lpMapAddress = (LPTSTR) MapViewOfFile(hMapFile,
    FILE_MAP_ALL_ACCESS,
    0,
    0,
    0);
  
    ReadFile(
    hFile,
    lpMapAddress,
    75,
    &amp;bytesRead,
    NULL);
  
    sprintf((char*)lpMapAddress, "&lt;output 1&gt;");
  
    printf((char*) lpMapAddress);
  </code></pre>
  
  <p>However, the printf call only returns "&lt; output 1 >" and not the contents of the file.</p>
  
  <p>EDIT:
  Found the problem. I'm writing to the input file when I call sprintf. But I still don't know why...</p>