如何防止我的函数在RemoveDirectory()WINAPI中延迟删除?

问题描述 投票:1回答:1

如何更正下面的代码才能使其正常工作?该功能的目的只是删除目录中的目录和文件。

因为当我运行这个函数时(即使是在一个单独的线程中),由于某些句柄存在(可能),它不会删除第一个参数中传递的文件夹。

并且Windows在程序结束前不会删除该文件夹,请参阅下面的代码。

如何在调用RemoveDirectory之前修复现有的句柄?

当调用RemoveDirectory时,我已经尝试将HANDLE变量(在下面的函数内部)移出堆栈。

问题只在于延迟删除文件夹(不是其他文件,它们正常删除)。

int DeleteDirectory(const std::string &refcstrRootDirectory,
                    bool              bDeleteSubdirectories = true)
{
  bool            bSubdirectory = false;       // Flag, indicating whether
                                               // subdirectories have been found
  HANDLE          hFile;                       // Handle to directory
  std::string     strFilePath;                 // Filepath
  std::string     strPattern;                  // Pattern
  WIN32_FIND_DATA FileInformation;             // File information


  strPattern = refcstrRootDirectory + "\\*.*";
  hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
  if(hFile != INVALID_HANDLE_VALUE)
  {
    do
    {
      if(FileInformation.cFileName[0] != '.')
      {
        strFilePath.erase();
        strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;

        if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
          if(bDeleteSubdirectories)
          {
            // Delete subdirectory
            int iRC = DeleteDirectory(strFilePath, bDeleteSubdirectories);
            if(iRC)
              return iRC;
          }
          else
            bSubdirectory = true;
        }
        else
        {
          // Set file attributes
          if(::SetFileAttributes(strFilePath.c_str(),
                                 FILE_ATTRIBUTE_NORMAL) == FALSE)
            return ::GetLastError();

          // Delete file
          if(::DeleteFile(strFilePath.c_str()) == FALSE)
            return ::GetLastError();
        }
      }
    } while(::FindNextFile(hFile, &FileInformation) == TRUE);

    // Close handle
    ::FindClose(hFile);

    DWORD dwError = ::GetLastError();
    if(dwError != ERROR_NO_MORE_FILES)
      return dwError;
    else
    {
      if(!bSubdirectory)
      {
        // Set directory attributes
        if(::SetFileAttributes(refcstrRootDirectory.c_str(),
                               FILE_ATTRIBUTE_NORMAL) == FALSE)
          return ::GetLastError();

        // Delete directory
        if(::RemoveDirectory(refcstrRootDirectory.c_str()) == FALSE)
          return ::GetLastError();
      }
    }
  }
  return 0;
}

附:这段代码来自这里:How to delete a folder in C++?

c++ winapi
1个回答
1
投票

首先,我为提出这样一个误导性的问题而道歉,但......

我发现了我的错误,它不在我在问题中发布的功能中。

我有一个项目,我使用我的函数DirectoryExists,并有一个标题“opendir”的函数(它是Windows的Linux头的可移植版本,其中可能有WINAPI HANDLEs)。我打开它之后忘记关闭目录以检查它是否存在。

bool DirectoryExists(const std::string & path)
{
    DIR *dir = opendir(path.c_str());
    if (dir) {
        return true;
    }
    else
        return false;
}

我纠正了这个错误:

bool DirectoryExists(const std::string & path)
{
    DIR *dir = opendir(path.c_str());
    closedir(dir);
    if (dir) {
        return true;
    }
    else
        return false;
}

我希望这个问题可以帮助某人理解为什么RemoveDirectory()可以像预期的那样工作。可能有一些你没有注意到的句柄,并不是那么清楚如果你不是一个专业的Windows程序员。

© www.soinside.com 2019 - 2024. All rights reserved.