FindNextFileA返回6(ERROR_INVALID_HANDLE)

时间:2014-02-04 07:15:34

标签: winapi visual-c++ console-application

有一个递归查找文件夹中所有文件的功能(存储在 logPath 文件夹名称末尾有'\')。它用于 64 位控制台应用程序(在MSVC 2008中编译),并在Win7 64 位操作系统上运行。

当我运行像“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);
}

2 个答案:

答案 0 :(得分:0)

我不能说为什么FindFirstFileA()会在使用控制台重定向时返回该特定错误代码(您没有显示如何将结果输出到控制台),但代码中存在一些小错误(大多数值得注意的是,您没有在FindFirstFileA()上执行足够的错误处理,也没有将\附加到目录路径的末尾,并递归调用FindFiles()

请改为尝试:

void FindFiles(const std::string &logPath, FileList& fileList)
{
    WIN32_FIND_DATAA fd;
    DWORD err;

    std::string sLogPath(logPath);
    if (sLogPath.length() > 0)
    {
        if (sLogPath[sLogPath.length()-1] != '\\')
            sLogPath += '\\';
    }

    HANDLE f = FindFirstFileA((sLogPath + "*").c_str(), &fd);
    if (f == INVALID_HANDLE_VALUE)
    {
        err = GetLastError();
        if (err != ERROR_FILE_NOT_FOUND)
        {
            printf("Unexpected error in FindFirstFileA(%s): %d\n", sLogPath.c_str(), err);
            fflush(stdout);
            abort();
        }

        printf("No files found at %s\n", sLogPath.c_str());
        return;
    }

    FileList dirList;
    do
    {
        if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            if ((strcmp(fd.cFileName, ".") != 0) && (strcmp(fd.cFileName, "..") != 0))
            {
                std::string path = sLogPath + fd.cFileName;
                printf("Processing dir %s\n", sLogPath.c_str());
                dirList.push_back(path);
            }
        }
        else
        {
            std::string path = sLogPath + fd.cFileName;
            printf("Processing file %s\n", path.c_str());
            fileList.push_back(path);
        }
    }
    while (FindNextFileA(f, &fd));

    err = GetLastError();
    FindClose(f);

    if (err != ERROR_NO_MORE_FILES)
    {
        printf("Unexpected error in FindNextFileA(%s): %d\n", sLogPath.c_str(), err);
        fflush(stdout);
        abort();
    }

    //for(FileList::const_iterator it = dirList.begin(); it != dirList.end(); ++it)
    //  FindFiles(*it, fileList);
}

答案 1 :(得分:0)

有一个boost::thread t我按HANDLE h = t.native_handle()

存储其句柄

稍后有一个电话CloseHandle(h)关闭它。

我忘记了,然后boost::~thread()析构函数也关闭它的句柄。

我复制螺纹手柄后 HANDLE h = NULL;
::DuplicateHandle(..., t.native_handle(), &h, ...)
关闭之前 - 问题消失了......

相关问题