有一个功能可以递归查找文件夹中的所有文件(存储在logPath文件夹名称的末尾有“ \”)。它用于64位控制台应用程序(在MSVC 2008中编译),并在Win7 64位OS上运行。
当我运行“ program.exe folder_to_find”之类的应用程序时,它运行良好。
当我运行“ program.exe folder_to_find >> result.txt”之类的应用程序时,它失败并返回错误6(ERROR_INVALID_HANDLE),返回FindNextFileA(即使没有对大文件夹进行递归调用)。例如,它可以在文件夹中找到前150个文件,而不是现有的240个文件。
void FindFiles(const std::string &logPath, FileList& fileList)
{
WIN32_FIND_DATAA fd;
HANDLE f = FindFirstFileA((logPath + "*").c_str(), &fd);
if (f == INVALID_HANDLE_VALUE)
{
printf("No files found at %s - %d\n", logPath.c_str(), GetLastError());
return;
}
FileList dirList;
do
{
if (strcmp(fd.cFileName, ".") == 0)
continue;
if (strcmp(fd.cFileName, "..") == 0)
continue;
std::string path = logPath + fd.cFileName;
printf("Processing %s\n", path.c_str());
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
dirList.push_back(path);
else
fileList.push_back(path);
}
while (FindNextFileA(f, &fd) != 0);
DWORD err = GetLastError();
if (err != ERROR_NO_MORE_FILES)
{
printf("Unexpected error in FindNextFileA(%s): %d\n", logPath.c_str(), err);
fflush(stdout);
abort();
}
FindClose(f);
//for(FileList::const_iterator it = dirList.begin(); it != dirList.end(); ++it)
// FindFiles(*it, fileList);
}
有一个boost::thread t
,我把它的句柄存放在HANDLE h = t.native_handle()
有人打来电话CloseHandle(h)
,稍后再关闭。
我忘记了,然后boost::~thread()
析构函数也关闭了它的句柄。
在
HANDLE h = NULL;
::DuplicateHandle(..., t.native_handle(), &h, ...)
关闭前我将线程句柄重复一遍之后-问题消失了...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句