将参数传递给FindNextFileA不起作用?

时间:2019-03-30 08:54:39

标签: c++ visual-c++

我以为我知道我正在使用这项技术,但是这次我必须做些不同的事情。

通过传递参数尝试FindNextFileA时出现错误。

// "$FndDirEnt_Sbr_Mod.cpp"

#include <stdio.h>

typedef struct {
    char        SchPth[MAX_PATH];           //  Path of directory to search
    char        DirFilNamFnd[MAX_PATH];     //  Name of directory or file found
    bool        DirFndInd;                  //  Directory was found indicator
    bool        NrmBitInd;                  //  Normal bit on indicator
    bool        ArcBitInd;                  //  Archive bit on indicator
    bool        HidBitInd;                  //  Hidden bit on indicator
    bool        RdoBitInd;                  //  Read-only bit on indicator
    DWORD       LstErr;                     //  Error code if not found

}FndFilInfStc;

bool FndFstDirEnt(HANDLE * FndFilHnd, FndFilInfStc * FndFilInf)
;

bool FndNxtDirEnt(HANDLE FndFilHnd, FndFilInfStc * FndFilInf)
;

#include "$FndDirEnt_Sbr_Pub.h"

#define BitFnd(ChkBit, BitChkFor) ((ChkBit & BitChkFor) == BitChkFor)

void FndDirEnt()
{
    HANDLE          FndFilHnd;
    FndFilInfStc    FndFilInf;
    bool            FndDirErr;


    strncpy_s(FndFilInf.SchPth, sizeof(FndFilInf.SchPth), "C:\\*.*", strlen("C:\\*.*"));
    FndDirErr = FndFstDirEnt(&FndFilHnd, &FndFilInf);
    if (FndDirErr)
        FndDirErr = FndNxtDirEnt(&FndFilHnd, &FndFilInf);
}

bool FndFstDirEnt(HANDLE * FndFilHnd, FndFilInfStc * FndFilInf)
{
    LPCSTR              lpFileName;
    FINDEX_INFO_LEVELS  fInfoLevelId = FindExInfoBasic;
    LPVOID              lpFindFileData;
    FINDEX_SEARCH_OPS   fSearchOp = FindExSearchNameMatch;
    LPVOID              lpSearchFilter = NULL;
    DWORD               dwAdditionalFlags = FIND_FIRST_EX_LARGE_FETCH;
    size_t              NbrChrCvt = 0;
    _WIN32_FIND_DATAA   FndFilDta;

    *FndFilHnd = INVALID_HANDLE_VALUE;
    *FndFilHnd = FindFirstFileExA(FndFilInf->SchPth, fInfoLevelId, &FndFilDta, fSearchOp, lpSearchFilter, dwAdditionalFlags);
    if (*FndFilHnd == INVALID_HANDLE_VALUE) {
        FndFilInf->LstErr = GetLastError();
        printf("Invalid handle for %s\n", FndFilInf->SchPth);
        return false;                   // **************** EARLY RETURN ****************
    }
    else {
        printf("%p\n", FndFilHnd);
        return true;
    }
}


bool FndNxtDirEnt(HANDLE FndFilHnd, FndFilInfStc * FndFilInf)
{
    _WIN32_FIND_DATAA   FndFilDta;
    bool                FndDirErr;

    printf("%p   schpth=%s\n", FndFilHnd, FndFilInf->SchPth);
    FndDirErr = FindNextFileA(FndFilHnd, &FndFilDta);           //  <-- error here
    return FndDirErr;
}


我遇到以下错误:

Exception thrown at 0x00007FFB564D9C90 (ntdll.dll) 
in $FndDirEnt_Tst.exe: 0xC0000005: Access violation reading location
0xFFFFFFFFFFFFFFFF.

我得到的输出是:

00000033228FF078
00000033228FF078   schpth=C:\*.*

我不知道可以添加哪些其他详细信息,但是尝试提交该问题时说我的帖子主要是代码,却一直出错。 TIA。

1 个答案:

答案 0 :(得分:1)

这里

FndDirErr = FndNxtDirEnt(&FndFilHnd, &FndFilInf);

您要传递句柄的地址。

句柄的地址不是有效的句柄。

&FndFilHnd删除&符。