title: CopyFileExW fails with 0 for an existing file
body: <p><strong>Context:</strong></p>
  <p>I have an application that searches files in a directory using <code>QDirIterator</code>, filters and copies specific files.</p>
  <p><strong>Problem:</strong></p>
  <p>Using the results from <a href="https://doc.qt.io/qt-5/qdiriterator.html#next" rel="nofollow noreferrer">QDirIterator::next()</a>, I ensure the file exists (as a unnecessary safe measure) using <a href="https://doc.qt.io/qt-5/qfile.html#exists" rel="nofollow noreferrer">QFile::exists(QString)</a> which is valid.</p>
  <p>However, when attempting to copy the file using <a href="https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-copyfileexw" rel="nofollow noreferrer">CopyFileExW</a>, it <a href="https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-copyfileexw#return-value" rel="nofollow noreferrer">returns 0</a> meaning the file copy failed. I have absolutely no idea why.</p>
  <p><strong>File location:</strong></p>
  <pre><code>C:/Users/CybeX/Documents/BLMS/Repo/BLMS-Work-Dev/Meeting 2 - Requirements Document Discussion &amp; Dev/2020-05-19 11.22.30 Someone Person's Zoom Meeting 98661954658/zoom_0.mp4
  </code></pre>
  <hr />
  <p><strong>Sanity Tests</strong></p>
  <p>I added some sanity tests to check the file content through the conversion from <code>QString</code> -&gt; <code>LPCWSTR</code> as <code>FileCopyExW</code> requires <code>LPCWSTR</code> and it is suggested to convert <code>QString -&gt; LPCWSTR</code> <a href="https://www.qtcentre.org/threads/65546-Can-t-convert-QString-to-LPCWSTR?p=289096#post289096" rel="nofollow noreferrer">here</a>.</p>
  <p><em>Regarding the conversion, I have tried <a href="https://stackoverflow.com/a/26366143/4628115">this</a> too but it yields the same result. It is also suggested as bad practice to user c-style casts</em></p>
  <p>Sanity tests (in application code below) all pass, but <code>FileCopyExW</code> always fails with:</p>
  <blockquote>
  <p>&quot;Error 0x80070002: The system cannot find the file specified.&quot;</p>
  </blockquote>
  <p>Code inside application:</p>
  <pre><code>QString m_src = QString(&quot;C:/Users/CybeX/Documents/BLMS/Repo/BLMS-Work-Dev/Meeting 2 - Requirements Document Discussion &amp; Dev/2020-05-19 11.22.30 Someone Person's Zoom Meeting 98661954658/zoom_0.mp4&quot;);
  QString m_dst = QString(&quot;C:/Users/CybeX/Documents/BLMS/Repo/BLMS-Work-Dev/Meeting 2 - Requirements Document Discussion &amp; Dev/2020-05-19 11.22.30 Someone Person's Zoom Meeting 98661954658/zoom_0.mp42&quot;);
       
  // Hard coded test location attempting to match variables' content below
  //    QString srcLocation = QString(&quot;C:/Users/CybeX/Documents/BLMS/Repo/BLMS-Work-Dev/Meeting 2 - Requirements Document Discussion &amp; Dev/2020-05-19 10.41.17 Someone Person's Zoom Meeting 96047275811/zoom_0.mp4&quot;);
  //    QString dstLocation = QString(&quot;C:/Users/CybeX/Documents/BLMS/Repo/BLMS-Work-Dev/Meeting 2 - Requirements Document Discussion &amp; Dev/2020-05-19 10.41.17 Someone Person's Zoom Meeting 96047275811/zoom_0.mp44&quot;);
  
  //    std::wstring srcWString1 = srcLocation.toStdWString();
  //    std::wstring dstWString1 = dstLocation.toStdWString();
  
  //    const wchar_t* localC_src1 = srcLocation.toStdWString().c_str();
  //    const wchar_t* localC_dst1 = dstLocation.toStdWString().c_str();
  //
  //    std::wstring srcWString = m_src.toStdWString();
  //    std::wstring dstWString = m_dst.toStdWString();
  
  
  // Used inside copy function
      const wchar_t* localC_src = m_src.toStdWString().c_str();
      const wchar_t* localC_dst = m_dst.toStdWString().c_str();
      
      // Sanity tests
       if (m_src.contains(&quot;96047275811/zoom_0.mp4&quot;)) {
            if (srcLocation != m_src) {
                 qDebug() &lt;&lt; &quot;Warning&quot;;            // Never gets hit
            }
            if (srcWString != srcWString1) {
                 qDebug() &lt;&lt; &quot;Warning&quot;;            // Never gets hit
            }
            if (*localC_src != *localC_src1) {
                 qDebug() &lt;&lt; &quot;Warning&quot;;            // Never gets hit
            }
  
            if (!QFile::exists(srcLocation)) {
                 qDebug() &lt;&lt; &quot;Warning&quot;;             // Never gets hit
            }
       }
       auto rc = CopyFileExW(localC_src, localC_dst, &amp;FileManager::copyProgress, this, &amp;bStopBackup, 0);
       if (rc == 0) {
            printWarning(TAG, QString(&quot;File Copy Error: %1&quot;).arg(getLastErrorMsg()));
            // copy failed
            return FileResult::IOError;               // This file always hits
       }
  
       // copy success
       return FileResult::Success;
  </code></pre>
  <p>However, hard coding the file location in a custom test application does indeed work correctly.</p>
  <p><strong>Testing Application:</strong></p>
  <p><em>Result is successful</em></p>
  <pre><code>static QString toString(HRESULT hr)
  {
       _com_error err{hr};
       const TCHAR* lastError = err.ErrorMessage();
       return QStringLiteral(&quot;Error 0x%1: %2&quot;).arg((quint32)hr, 8, 16, QLatin1Char('0'))
              .arg(lastError);
  }
  
  static QString getLastErrorMsg()
  {
       QString s = toString(HRESULT_FROM_WIN32(GetLastError()));
       return s;
  }
  
  int main(int argc, char* argv[])
  {
       QCoreApplication a(argc, argv);
  
       QString m_src = QString(&quot;C:/Users/CybeX/Documents/BLMS/Repo/BLMS-Work-Dev/Meeting 2 - Requirements Document Discussion &amp; Dev/2020-05-19 11.22.30 Someone Person's Zoom Meeting 98661954658/zoom_0.mp4&quot;);
       QString m_dst = QString(&quot;C:/Users/CybeX/Documents/BLMS/Repo/BLMS-Work-Dev/Meeting 2 - Requirements Document Discussion &amp; Dev/2020-05-19 11.22.30 Someone Person's Zoom Meeting 98661954658/zoom_0.mp42&quot;);
  
       auto rc = CopyFileExW(m_src.toStdWString().c_str(), m_dst.toStdWString().c_str(),
                             NULL, NULL, NULL, 0);
       if (rc == 0) {
            QString s = getLastErrorMsg();
            // copy failed
            qDebug() &lt;&lt; &quot;Failed&quot;;
       }
       else {
            qDebug() &lt;&lt; &quot;Copied&quot;;                    // Always gets hit
       }
  
       // copy success
       return a.exec();
  }
  </code></pre>