title: CreateFileMapping returns ERROR_INVALID_HANDLE
body: <p>I am trying to use CreateFileMapping for the first time and it is giving me this error when I use GetLastError():</p>
  
  <pre><code>ERROR_INVALID_HANDLE: The handle is invalid.
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>    // create the name of our file-mapping object
      nTry++; // Ensures a unique string is used in case user closes and reopens
      wsprintfA(szName, FS6IPC_MSGNAME1 ":%X:%X", GetCurrentProcessId(), nTry);
      // stuff the name into a global atom
      m_atom = GlobalAddAtomA(szName);
      if (m_atom == 0)
     {    *pdwResult = ERR_ATOM;
          return FALSE;
      }
  
      // create the file-mapping object
      m_hMap = CreateFileMappingA(
                      (HANDLE)0xFFFFFFFF, // use system paging file
                      NULL,               // security
                      PAGE_READWRITE,     // protection
                      0, MAX_SIZE+256,       // size
                     szName);            // 
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>The first issue was resolved, but now my program crashes somewhere else.</p>
  
  <pre><code>#define FS6IPC_MESSAGE_SUCCESS 1
  #define FS6IPC_MESSAGE_FAILURE 0
  
  // IPC message types
  #define FS6IPC_READSTATEDATA_ID    1
  #define FS6IPC_WRITESTATEDATA_ID   2
  
  // read request structure
  typedef struct tagFS6IPC_READSTATEDATA_HDR
  {
    DWORD dwId;       // FS6IPC_READSTATEDATA_ID
    DWORD dwOffset;   // state table offset
    DWORD nBytes;     // number of bytes of state data to read
    void* pDest;      // destination buffer for data (client use only)
  } FS6IPC_READSTATEDATA_HDR;
  
  // write request structure
  typedef struct tagFS6IPC_WRITESTATEDATA_HDR
  {
    DWORD dwId;       // FS6IPC_WRITESTATEDATA_ID
    DWORD dwOffset;   // state table offset
    DWORD nBytes;     // number of bytes of state data to write
  } FS6IPC_WRITESTATEDATA_HDR;
  </code></pre>
  
  <hr>
  
  <pre><code>while (*pdw)
  {   switch (*pdw)
      {   case FS6IPC_READSTATEDATA_ID:
              pHdrR = (FS6IPC_READSTATEDATA_HDR *) pdw;
              m_pNext += sizeof(FS6IPC_READSTATEDATA_HDR);
              if (pHdrR-&gt;pDest &amp;&amp; pHdrR-&gt;nBytes)
                  CopyMemory(pHdrR-&gt;pDest, m_pNext, pHdrR-&gt;nBytes);
              m_pNext += pHdrR-&gt;nBytes; // Debugger says the issue is here
              break;
  
          case FS6IPC_WRITESTATEDATA_ID:
              // This is a write, so there's no returned data to store
              pHdrW = (FS6IPC_WRITESTATEDATA_HDR *) pdw;
              m_pNext += sizeof(FS6IPC_WRITESTATEDATA_HDR) + pHdrW-&gt;nBytes;
              break;
  
          default:
              // Error! So terminate the scan
              *pdw = 0;
              break;
      }
  
      pdw = (DWORD *) m_pNext;
  }
  </code></pre>