title: SendMessage WM_COPYDATA between two processes
body: <p>I have to pass a string from one process to another, so I send <code>WM_COPYDATA</code> message with the relevant information. The receiver process waits by a <code>while</code> loop and sleeps for 500ms until the message received.</p>
  
  <p>I don't know why it doesn't work. The sender process stops on <code>SendMessage</code>, because the receiver process doesn't get the message. When the loop on the receiver ends because the timeout, and the receiver process ends too, then the <code>SendMessage</code> of the sender ends and it can continue running.</p>
  
  <p>Here is the relevant code. The sender process:</p>
  
  <pre><code>COPYDATASTRUCT cp;
  memset(&amp;cp, 0, sizeof(COPYDATASTRUCT));
  cp.cbData = 128;
  cp.lpData = messageStr;
  cp.dwData = 45382;
  ::SendMessage(receiverHWND, WM_COPYDATA, (WPARAM)0, (LPARAM)&amp;cp);
  </code></pre>
  
  <p>The receiver process:</p>
  
  <pre><code>// Define which function runs when a message WM_COPYDATA is sent
  CopyDataHandler = onCopyData;
  
  void onCopyData(COPYDATASTRUCT *cp)
  {
      if (cp-&gt;lpData == NULL)
          return;
  
      if (cp-&gt;dwData == 45382) // means a message from the sender
      {
          strcpy(WinprocMessage, cp-&gt;lpData); 
          return;
      }
  }
  
  BOOL OnCopyData(HWND hWnd, HWND hwndFrom, PCOPYDATASTRUCT pcds)
  {
      if(CopyDataHandler != NULL)
          CopyDataHandler(pcds);
  
      return TRUE;
  }
  
  LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
      switch (msg)
      {
          HANDLE_MSG (hWnd, WM_COPYDATA, OnCopyData);
      }
      return DefWindowProc(hWnd, msg, wParam, lParam);
  }
  </code></pre>
  
  <p><b>(Pay attention</b> that there are two functions: onCopyData and OnCopyData)</p>
  
  <p>Now the code of the loop:</p>
  
  <pre><code>    _num attempts = procTimeout * 2;
      while ((attempts-- &gt; 0) &amp;&amp; (WinprocMessage[0] == '\0'))
          Sleep(500);
  </code></pre>
  
  <p>So, why doesn't it work? is <code>Sleep</code> function blocks other processes to <code>SendMessage</code>? </p>
  
  <p>In addition, this code works fine with another sender process. (The other one is huge and I don't know it all, can't guess where the difference exists) Maybe the sender process have to <code>RegisterWindowMessage</code> somehow?</p>