bool ApplicationLaunch::nativeEvent(const QByteArray & eventType,
                                    void * msg,
                                    long * result)
{
   MSG* message = (MSG*)msg;
   if (message->message == WM_COPYDATA)
   {
      COPYDATASTRUCT* cds = reinterpret_cast<COPYDATASTRUCT*>(message->lParam);
      if (cds->dwData == OPENFILE)
      {
         QString fileName = QString::fromUtf8(
               reinterpret_cast<char*>(cds->lpData),
               cds->cbData);
         openFileRequest(fileName);
         *result = 1;
         return true;
      }
   }
   else if (message->message == wmGetMainWindowHandle())
   {
      if (pMainWindow_)
         *result = reinterpret_cast<LRESULT>((HWND)(pMainWindow_->winId()));
      else
         *result = 0;
      return true;
   }
   return QWidget::nativeEvent(eventType, message, result);
}
bool ApplicationLaunch::winEvent(MSG *message, long *result)
{
   if (message->message == WM_COPYDATA)
   {
      COPYDATASTRUCT* cds = reinterpret_cast<COPYDATASTRUCT*>(message->lParam);
      if (cds->dwData == OPENFILE)
      {
         QString fileName = QString::fromUtf8(
               reinterpret_cast<char*>(cds->lpData),
               cds->cbData);
         openFileRequest(fileName);
         *result = 1;
         return true;
      }
   }
   else if (message->message == wmGetMainWindowHandle())
   {
      if (pMainWindow_)
         *result = reinterpret_cast<LRESULT>(pMainWindow_->winId());
      else
         *result = NULL;
      return true;
   }
   return QWidget::winEvent(message, result);
}
bool ApplicationLaunch::sendMessage(QString filename)
{
   if (acquireLock())
      return false;

   HWND hwndAppLaunch = NULL;
   do
   {
      hwndAppLaunch = ::FindWindowEx(HWND_MESSAGE, hwndAppLaunch, NULL, WINDOW_TITLE);
   } while (hwndAppLaunch == (HWND)winId()); // Ignore ourselves

   if (::IsWindow(hwndAppLaunch))
   {
      HWND hwnd = reinterpret_cast<HWND>(::SendMessage(hwndAppLaunch,
                                                       wmGetMainWindowHandle(),
                                                       0,
                                                       0));
      if (::IsWindow(hwnd))
      {
         HWND hwndPopup = ::GetLastActivePopup(hwnd);
         if (::IsWindow(hwndPopup))
            hwnd = hwndPopup;
         ::SetForegroundWindow(hwnd);
         if (::IsIconic(hwnd))
            ::ShowWindow(hwnd, SW_RESTORE);

         if (!filename.isEmpty())
         {
            QByteArray data = filename.toUtf8();

            COPYDATASTRUCT copydata;
            copydata.dwData = OPENFILE;
            copydata.lpData = data.data();
            copydata.cbData = data.size();

            HWND sender = (HWND)winId();

            ::SendMessage(hwndAppLaunch,
                          WM_COPYDATA,
                          reinterpret_cast<WPARAM>(sender),
                          reinterpret_cast<LPARAM>(&copydata));
         }
      }
   }

   return true;
}