title: Inter-process communication with SendMessage on Windows and Qt5
body: <p>My problem is, I suppose, reasonably common: opening a file in the first instance of my app when a second instance is run (e.g. by opening an asociated file in the explorer).</p>
  
  <p>The way I implemented this on Windows is by using the <code>SendMessage</code> Win API and receiving the message by reimplementing winEvent in a Qt window. This worked well enough on Qt4. But for some reason, it stopped working completely after I had updated my app to Qt 5.</p>
  
  <p>I have written a minimal test (see below) that reproduces the behaviour: also fine on Qt 4, but not working on Qt 5 (the message is not received). I am using <code>mingw32</code> (<code>gcc</code>) in case it makes any difference. I am very unfamiliar with the Windows API so would be delighted if someone could explain the odd behaviour.</p>
  
  <p>Thanks a lot!</p>
  
  <p>server.c:</p>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  #include &lt;windows.h&gt;
  #include &lt;winuser.h&gt;
  #include &lt;windef.h&gt;
  
  #include &lt;QApplication&gt;
  #include &lt;QLabel&gt;
  #include &lt;QDialog&gt;
  
  #include "winmessagelistener.h"
  
  bool WinMessageListener::winEvent( MSG* message, long* result ) {
      if( message-&gt;message == WM_COPYDATA ) {
          label-&gt;setText( "Message!" );
          // We process the event here
          *result = 0;
          return true;
      }
      else {
          // Give the event to qt
          return false;
      }
  }
  
  WinMessageListener::WinMessageListener() : QDialog() {
      setWindowTitle( "blah" );
      label = new QLabel( this );
      label-&gt;setText("no message");
  }
  
  int main(int argc, char **argv) {
      QApplication app (argc, argv);
          WinMessageListener listener;
  
      listener.show();
  
      return app.exec();
  }
  </code></pre>
  
  <p>winmessagelistener.h:</p>
  
  <pre><code>#include &lt;QDialog&gt;
  #include &lt;QLabel&gt;
  
  class WinMessageListener : public QDialog {
      Q_OBJECT
  
      public:
          WinMessageListener();
  
      private:
          // Override the default event message
          bool winEvent( MSG* message, long* result );
          QLabel* label;
  };
  </code></pre>
  
  <p>client.c:</p>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  #include &lt;windows.h&gt;
  #include &lt;winuser.h&gt;
  #include &lt;windef.h&gt;
  
  #include &lt;iostream&gt;
  #include &lt;QString&gt;
  
  const QString WINDOW_TITLE = "blah";
  
  int main(int argc, char **argv) {
      LPCWSTR window_title = (LPCWSTR) WINDOW_TITLE.utf16();
      HWND window_handle_ = FindWindow( NULL, window_title );
  
      std::cerr &lt;&lt; "Window handle = " &lt;&lt; window_handle_ &lt;&lt; std::endl;
  
      COPYDATASTRUCT data = { 0, 0, 0 };
  
      SendMessage( window_handle_, WM_COPYDATA, 0, (LPARAM) &amp;data );
  }
  </code></pre>