2012-08-02 110 views
1

下面的代碼產生了運行時錯誤:搜索文件使用擴展功能

Unhandled exception at 0x773315de in Window File Search.exe: 0xC0000005: Access violation.

我沒有什麼原因造成任何的想法。你能指出我的錯誤嗎?

下面是函數,可能包含的罪魁禍首:

int fileSearcher::findFilesRecursivelly(const TCHAR* curDir,const TCHAR* fileName,bool caseSensitive,TCHAR* output) 
{ 
    HANDLE hFoundFile; 
    WIN32_FIND_DATA foundFileData; 

    TCHAR nextDirBuffer[MAX_PATH]=TEXT(""); 

    SetCurrentDirectory(curDir); 
    //Fetch inside current directory 
    hFoundFile = FindFirstFileEx(fileName,FINDEX_INFO_LEVELS::FindExInfoBasic,&foundFileData ,FINDEX_SEARCH_OPS::FindExSearchNameMatch ,NULL , FIND_FIRST_EX_LARGE_FETCH); 

    if(hFoundFile != INVALID_HANDLE_VALUE) 
    {  
     do 
     { 
      nothingFound = false; 

      wcscat(output,curDir); 
      wcscat(output,TEXT("\\")); 
      wcscat(output,foundFileData.cFileName); 
      wcscat(output,TEXT("\n")); 
     } 
     while(FindNextFile(hFoundFile,&foundFileData)); 
    } 

    //Go to the subdirs 
    hFoundFile = FindFirstFileEx(TEXT("*"),FINDEX_INFO_LEVELS::FindExInfoBasic,&foundFileData ,FINDEX_SEARCH_OPS::FindExSearchLimitToDirectories ,NULL , FIND_FIRST_EX_LARGE_FETCH); //This line of code was on the call stack 

    if(hFoundFile != INVALID_HANDLE_VALUE) 
    { 
     do 
     { 
      wcscat(nextDirBuffer,curDir); 
      wcscat(nextDirBuffer,TEXT("\\")); 
      wcscat(nextDirBuffer,foundFileData.cFileName); 
      findFilesRecursivelly(nextDirBuffer,fileName,caseSensitive,outputBuffer); 
     } 
     while(FindNextFile(hFoundFile,&foundFileData)); 
    } 

    return 0; 

} 

不太重要的代碼:

文件Search.h

#ifndef UNICODE 
#define UNICODE 
#endif 

#include <Windows.h> 

namespace fileSearch 
{ 
class fileSearcher 
{ 
public: 
    fileSearcher(); 

    void getAllPaths(const TCHAR* fileName,bool caseSensitive,TCHAR* output); 
    /*Returns all matching pathes at the current local system. Format: 
    [A-Z]:\[FirstPath\foo1...\fileName] 
    [A-Z]:\[SecondPath\foo2...\fileName] 
    [A-Z]:\[ThirdPath\foo3...\fileName] 
    ... 
    [A-Z]:\[FourthPath\foo4...\fileName] 
    Also an asterisk sign is supported, as in regular expressions. 

    This functions uses WinApi methods. 
    */ 

    int findFilesRecursivelly(const TCHAR* curDir,const TCHAR* fileName,bool caseSensitive,TCHAR* output); 
    //Searches for the file in the current and in sub directories. NOT IN PARENT!!!!!!!!!!!!!!!!!!!!! Returns true if the file is found. 

private: 
    static const int MAX_NUMBER_OF_FILES = 100; 
    static const int MAX_OUTPUT_SIZE = 2000; 

    bool nothingFound; 

    TCHAR outputBuffer[MAX_OUTPUT_SIZE]; 
}; 
} 

...休息FileSeach.cpp

#ifndef UNICODE 
#define UNICODE 
#endif 

#include "File Search.h" 

using namespace fileSearch; 

fileSearcher::fileSearcher() 
{ 
    nothingFound = true; 
} 

void fileSearcher::getAllPaths(const TCHAR* fileName,bool caseSensitive, TCHAR* output) 
{ 
    TCHAR localDrives[50]; 
    TCHAR currentDrive; 
    int voluminesChecked=0; 

    TCHAR searchedVolumine[5]; 


    GetLogicalDriveStrings(sizeof(localDrives)/sizeof(TCHAR),localDrives); 

    //For all drives: 
    for(int i=0; i < sizeof(localDrives)/sizeof(TCHAR); i++) 
    {  
      if(localDrives[i] >= 65 && localDrives[i] <= 90) 
      { 
       currentDrive = localDrives[i]; 
       voluminesChecked++; 
      } 
      else continue; 



    searchedVolumine[0] = currentDrive; 
    searchedVolumine[1] = L':'; 
    searchedVolumine[2] = 0; 



    outputBuffer[0]=0; 
    findFilesRecursivelly(searchedVolumine,fileName,caseSensitive,outputBuffer); 

    (nothingFound) ? wcscpy(output,L"File not found") : wcscpy(output,outputBuffer); 

    } 

} 

編輯

CURDIR的值某些迭代後 -

 +  curDir 0x003df234 "C:\.\$Recycle.Bin\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\." const wchar_t *

,但我不知道爲什麼。

+0

你試過你的調試器嗎?你是否抓住了例外以獲得更多細節? – nabulke 2012-08-02 19:41:48

+0

你有沒有試過在調試器下運行它? – 2012-08-02 19:43:31

+0

是的!這似乎是一個邏輯錯誤,這是一個debbuger自動檢測不到的。 – 0x6B6F77616C74 2012-08-02 19:47:21

回答

2

每個非根目錄包含自身(「。」)及其父(「..」)。你需要明確地排除那些遞歸搜索:

if (wcscmp(foundFileData.cFileName, L".") != 0 
    && wcscmp(foundFileData.cFileName, L"..") != 0) 
{ 
    wcscat(nextDirBuffer,curDir); 
    wcscat(nextDirBuffer,TEXT("\\")); 
    wcscat(nextDirBuffer,foundFileData.cFileName); 
    findFilesRecursivelly(nextDirBuffer,fileName,caseSensitive,outputBuffer); 
} 
2

看起來像緩衝區溢出。在通過目錄樹進行遞歸時,您忘記了每個目錄都包含對自身的引用(名稱是'。'),並且引用了它的父目錄(名稱是'..'),您必須從遞歸中排除這些引用。所以這樣做

do 
    { 
     if (wcscmp(foundFileData.cFileName, TEXT(".") == 0 || 
      wcscmp(foundFileData.cFileName, TEXT("..") == 0) 
     { 
      continue; 
     } 
     wcscat(nextDirBuffer,curDir); 
     wcscat(nextDirBuffer,TEXT("\\")); 
     wcscat(nextDirBuffer,foundFileData.cFileName); 
     findFilesRecursivelly(nextDirBuffer,fileName,caseSensitive,outputBuffer); 
    } 
    while(FindNextFile(hFoundFile,&foundFileData)); 

你編碼的方式,你只是循環和循環在同一目錄。