Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the FindFirstFile/FindNextFile does not list the entire content of the system directory on Windows 7?

I have a little question. The API FindNextFile does not list the entire contents of the directory C:\Windows\System32 (Windows 7 only). Anyone have any solution?

Code [Delphi]:

Var
  sAtr:       String;
  sPathName:  String;
  I:          Integer;
  iCont:      Integer;
  tHnd:       THandle;
  tArrAtr:    TStringList;
  tWDF:       WIN32_FIND_DATA;
Begin
  iCont := 0;
  sAtr := '';
  Result := TStringList.Create;
  tArrAtr := TStringList.Create;
  tHnd := FindFirstFile(PChar(sPath + '*.*'), tWDF);

  If RightStr(sPath, 1) <> '\' Then
    sPath := sPath + '\';

  If tHnd = INVALID_HANDLE_VALUE Then
    Exit;

  Repeat
    If (tWDF.dwFileAttributes And FILE_ATTRIBUTE_ARCHIVE) > 0 Then
      If (String(tWDF.cFileName[0]) <> '.') Then
      Begin
        sPathName := sPath + String(tWDF.cFileName);

        Result.Add(String(tWDF.cFileName) + sDel +
                   GetFileSizeAPI(sPathName));
        sAtr := '';
        Inc(iCont);
      End;
  Until (FindNextFile(tHnd, tWDF) <> True);

  //CloseHandle(tHnd);
like image 452
Jhonjhon_123 Avatar asked Nov 18 '25 12:11

Jhonjhon_123


1 Answers

I bet that you have a 64 bit machine and a 32 bit process. The File System Redirector comes in to play and System32 redirects to SysWOW64.

The best way to avoid the redirector is to execute a 64 bit process. Or you could list Sysnative to get the 64 bit system folder from a 32 bit process. You can even disable the File System Redirector but that is quite a dangerous thing to do and I would not recommend it.

Also, you tidy up the find handle by calling FindClose rather than CloseHandle. You should be adding the backslash before calling FindFirstFile. And test for special . and .. by comparing the full name against those special values.

like image 138
David Heffernan Avatar answered Nov 20 '25 05:11

David Heffernan