c++ - 如何修复 CopyFile() 错误 5 - 访问被拒绝错误

标签 c++ windows winapi copy

我正在尝试编写一个可以在 Linux 和 Windows 上使用的复制文件功能。它适用于 Linux,但在 Windows 上,我在尝试使用 WinApi 函数 CopyFile() 时收到错误代码 5。

在头文件.h中

这是文件命名空间中的自定义函数,我应该能够在 Linux 和 Windows 上使用它。

class File
{
public:
    static bool copyFile(std::string source, std::string destination);
private:
}

在 File.cpp 中

对于 Linux,这很简单:

#ifdef __unix__
  #include "File.h"
  bool File::copyFile(std::string source, std::string destination)
  {
      std::string arg = source + " " + destination;
      return launchProcess("cp", arg);
  }
#endif

在 Windows 特定的代码块中,我使用了 WinAPI (#include < windows.h >) 函数 CopyFile()。这接受 LPCWSTR 数据类型而不是字符串。为了克服这个问题,我创建了一个将字符串转换为 LPCWSTR 类型的函数。

#ifdef _WIN32    
  #include "File.h"
  #include <Windows.h>
  std::wstring strtowstr(const std::string &str)
  {
      // Convert an ASCII string to a Unicode String
      std::wstring wstrTo;
      wchar_t *wszTo = new wchar_t[str.length() + 1];
      wszTo[str.size()] = L'\0';
      MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wszTo,(int)str.length());
      wstrTo = wszTo;
      delete[] wszTo;
      return wstrTo;
  }


  bool File::copyFile(std::string source, std::string destination)
  {
      std::wstring wsource = strtowstr(source);
      std::wstring wdestination = strtowstr(destination);

      int result = CopyFileW(wsource.c_str(), wdestination.c_str(), TRUE);

          //for debugging...
          std::wcout << "The error is " << GetLastError() <<std::endl;
          std::wcout  << wsource.c_str() << std::endl;
          std::wcout << wdestination.c_str() << std::endl;              

      if (result == 0)
      {
          return false;
      }
      return true;
  }
#endif

在我的测试程序中

TEST(all,main_copy_file)
{
    std::cout << "Testing copyFile() function..." << std::endl;

    std::string srcDir = File::currentWorkingDirectory() + "srcDir";
    File::makeDirectory(srcDir);

    std::string destDir = File::currentWorkingDirectory() + "destDir/";
    File::makeDirectory(destDir);

    File::makeFile(srcDir, "testFile", ".txt");

    ASSERT_TRUE(File::fileExists(srcDir + "/testFile.txt")) << "Error: Test file has not been generated" << std::endl;
    ASSERT_TRUE(File::directoryExists(destDir)) << "Error: Destination directory does not exist" <<std::endl;

    ASSERT_TRUE(File::copyFile(srcDir + "/testFile.txt", destDir)) << "Error: Coppy unsucsessfull" << std::endl;
    ASSERT_TRUE(File::fileExists(destDir + "/testFile.txt")) << "Error: CoppyFile() flagged as sucsessfull but file does not exist" << std::endl;

}

在应用程序输出中(在 Windows 上)

/*
Testing copyFile() function...
The error is 5    
C:\GIT\CorteX\Externals\OSAL\build\Debug/srcDir/testFile.txt
C:\GIT\CorteX\Externals\OSAL\build\Debug/destDir/

error: Value of: File::copyFile(srcDir + "/testFile.txt", destDir)
Actual: false
Expected: true
Error: Coppy unsucsessfull
*/

错误代码 5 是拒绝访问错误。我认为当目录不存在、目录在其他地方打开或我没有权限时会出现此错误。

既然我测试过该目录确实存在,我想它一定是后两者之一。我可能只有有限的管理员权限(我不知道),但我可以在没有管理员权限的情况下粘贴到“destDir”。所以也许它认为目录是开放的?是否存在确保目录已关闭的命令?

在Linux上运行测试成功。

最佳答案

CopyFile API 需要源文件和目标文件的文件名。您的代码传递目标的目录名称。这会导致 API 失败。您还需要附加目标的文件名。

除此之外,您的代码还有其他几个问题:

  • Windows 上的路径分隔符是反斜杠 ( \ )。您正在混合使用正斜杠 ( /) 和反斜杠。根据传递的参数,系统不会将正斜杠转换为反斜杠,然后再将它们传递给较低级别​​的文件 I/O API。
  • 您正在调用 GetLastError为时已晚。您需要立即调用它,只要它被记录为返回一个有意义的值。不要将它与任何 其他代码穿插在一起,无论它在您看来多么微不足道。该代码可以修改并使调用线程的最后一个错误代码无效。
  • 您的代码采用 ASCII 编码的字符串。当处理包含非 ASCII 字符的文件时,这将停止工作。这很常见。
  • new wchar_t[...] std::vector<wchar_t> 什么都不给你买,除了引入错误的可能性。
  • 你的 MultiByteToWideChar -based 的字符串转换实现对不同字符编码的代码单元要求做出(不适当的)假设。这些假设可能不正确。通过传递 0 让 API 计算并告诉您目标缓冲区大小对于 cchWideChar
  • 您的字符串转换例程会忽略所有返回值,从而极有可能出现错误,并且不必要地难以诊断。

关于c++ - 如何修复 CopyFile() 错误 5 - 访问被拒绝错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45105179/

相关文章:

c++ - 使用 Visual Studio 的损坏构建

c++ - dynamic_cast 工作时程序应该失败/崩溃

windows - 从目录子树中的每个文本文件中提取第一行

windows - 如何从另一个 tcl 文件调用一个 tcl 文件

C++:将控制台窗口设置为 WS_POPUP

c++ - 复制文件错误代码 2

delphi - 如何从具有管理员权限的应用程序接收键盘输入到非管理员应用程序?

C++ 最令人头疼的解析又来了

c++ - 最快的 stride-3 收集指令序列是什么?

c++ - mingw:从 lambda 到函数指针的无效转换