简体   繁体   English

使用 FindNextFile 时按句柄获取搜索路径?

[英]Get search path by handle when using FindNextFile?

I use FindFirstFile() and FindNextFile() to list files of a directory.我使用FindFirstFile()FindNextFile()来列出目录的文件。 When I call FindFirstFile() , I have to give a search path to it.当我调用FindFirstFile()时,我必须为其提供搜索路径。 It returns a handle that can be used by FindNextFile() .它返回一个可供FindNextFile()使用的句柄。 Is there a WinAPI function that can get the previously given path by the handle?有没有 WinAPI function 可以通过句柄得到之前给定的路径?

Just store that information like you store the search handle already: in a variable.只需存储该信息,就像您已经存储搜索句柄一样:在一个变量中。 Then create your own wrapper functions for both FindFirstFileA() and FindNextFileA() :然后为FindFirstFileA()FindNextFileA()创建自己的包装函数:

type
  // What you want to give back per file system object
  TMyFindInfo= record  // Whatever you want to do here on your own
    wfd: Windows.WIN32_FIND_DATAA;  // Just provide this as-is because it already everything
  end;

  // Not only storing the handle, but also other details
  TMyFindHandle= record
    h: THandle;  // Search resource
    sFilter: String;  // Original query
    iMatches,  // How often did the search yield a file system object?
    iError: Cardinal;  // Which error has occured? 0=ERROR_SUCCESS.
  end;

function MyFindFile1st
( const sFilter: String
; out vInfo: TMyFindInfo
): TMyFindHandle;
begin
  result.sFilter:= sFilter;
  result.h:= Windows.FindFirstFileA( PChar(sFilter), vInfo.wfd );
  if result.h= INVALID_HANDLE_VALUE then begin
    result.iError:= Windows.GetLastError();
    case result.iError of
      ERROR_FILE_NOT_FOUND: ;  // The only error we don't need to display
    else  // Most likely ERROR_PATH_NOT_FOUND
      Windows.MessageBoxA
      ( Form1.Handle
      , PChar('Error initializing search "'+ result.sFilter
        + '": 0x'+ IntToHex( result.iError, 8 ))  // Get text message elsewhere
      , PChar('Error')
      , MB_ICONSTOP
      );
    end;

    result.iMatches:= 0;
    ZeroMemory( @vInfo, SizeOf( vInfo ) );  // Nothing to see here
  end else begin
    result.iError:= ERROR_SUCCESS;
    result.iMatches:= 1;
  end;
end;

function MyFindFile2nd
( var vHandle: TMyFindHandle
; out vInfo: TMyFindInfo
): Boolean;
begin
  result:= Windows.FindNextFileA( vHandle.h, vInfo.wfd );
  if not result then begin
    vHandle.iError:= Windows.GetLastError();
    case vHandle.iError of
      ERROR_SUCCESS,  // The only errors we don't need to display
      ERROR_NO_MORE_FILES: ;
    else
      Windows.MessageBoxA
      ( Form1.Handle
      , PChar('Error during search "'+ vHandle.sFilter  // Original filter from 1st call
        + '" after '+ IntToStr( vHandle.iMatches )+ ' elements occured: 0x'
        + IntToHex( vHandle.iError, 8 ))
      , PChar('Error')
      , MB_ICONSTOP
      );
    end;
    Windows.ZeroMemory( @vInfo, SizeOf( vInfo ) );  // Nothing to see here

    if not Windows.FindClose( vHandle.h ) then begin  // Release resource
      vHandle.iError:= Windows.GetLastError();
      case vHandle.iError of
        ERROR_SUCCESS: ;
      else  // Yes, this can fail, too
        Windows.MessageBoxA
        ( Form1.Handle
        , PChar('Error finalizing search "'+ vHandle.sFilter  // Original filter from 1st call
          + '" after '+ IntToStr( vHandle.iMatches )+ ' elements occured: 0x'
          + IntToHex( vHandle.iError, 8 ))
        , PChar('Error')
        , MB_ICONSTOP
        );
      end;
    end;
  end else Inc( vHandle.iMatches );  // One more match
end;


// Now the example on how to use it
procedure TForm1.Button1Click(Sender: TObject);
var
  vHandle: TMyFindHandle;
  vInfo: TMyFindInfo;
begin
  vHandle:= MyFindFile1st( 'C:\Windows\*.exe', vInfo );
  while vHandle.iError= ERROR_SUCCESS do begin
    Memo1.Lines.Add( vInfo.wfd.cFileName );
    MyFindFile2nd( vHandle, vInfo );  // Don't even need the Boolean result here
  end;
  Memo1.Lines.Add( '= '+ IntToStr( vHandle.iMatches )+ ' FS objects' );  // Not only files
end;

At no time there is a need to re-request a detail by handle, because you can keep that detail right with the handle that you need to take care of anyway.任何时候都不需要通过 handle 重新请求详细信息,因为您可以通过无论如何需要处理的 handle 保持该详细信息正确。 Just put both together into a record and pass that to your own functions.只需将两者放在一起记录并将其传递给您自己的函数即可。

My code is for demonstration purposes (although I think it's a rather trivial overall case).我的代码用于演示目的(尽管我认为这是一个相当微不足道的整体案例)。 I discourage from displaying dialog windows right in those functions, but instead react upon what vHandle.iError contains where I called those functions.我不鼓励在这些函数中直接显示对话框 windows,而是对我调用这些函数的地方包含的vHandle.iError做出反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM