Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Buscar un archivo sin FindFirst (ejemplo) (https://www.clubdelphi.com/foros/showthread.php?t=80859)

cHackAll 05-04-2008 00:30:35

Buscar un archivo sin FindFirst (ejemplo)
 
Código Delphi [-]
unit Unit1;

interface

uses
 Forms, Messages, ExtCtrls, StdCtrls, Classes, Controls, Windows, Dialogs;

type
 TForm1 = class(TForm)
  Edit1: TEdit;
  Edit2: TEdit;
  Timer1: TTimer;
  Button1: TButton;
  procedure Button1Click(Sender: TObject);
  procedure Timer1Timer(Sender: TObject);
 private
  procedure SearchEnd(var Msg: TMessage); message WM_USER;
 public
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

function SearchTreeForFile(RootPath, InputPathName, OutputPathBuffer: PChar): LongBool; stdcall; external 'imagehlp';
var Buffer: array [0..194] of Cardinal;
function Thread(hWnd: Cardinal): Integer; stdcall;
begin
 SendMessage(hWnd, WM_USER, Integer(SearchTreeForFile(@Buffer[65], @Buffer[130], @Buffer)), Integer(@Buffer));
end;

procedure Search(Handle: Cardinal; Path, FileName: string);
begin
 lstrcpy(@Buffer[65], @Path[1]);
 lstrcpy(@Buffer[130], @FileName[1]);
 CreateThread(nil, 0, @Thread, Pointer(Handle), 0, PDWORD(0)^);
end;

procedure TForm1.SearchEnd(var Msg: TMessage);
begin
 Timer1.Enabled := False;
 if LongBool(Msg.WParam) then
  ShowMessage(PChar(Msg.LParam));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 Search(Handle, PChar(Edit1.text), PChar(Edit2.text));
 Timer1.Enabled := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 if Length(Caption) > 12 then
  Caption := 'Buscando';
 Caption := Caption + '.';
end;

end.

La API SearchTreeForFile realiza la búsqueda de un archivo en una ruta específica, sin embargo el proceso que invoca a dicha API se detiene mientras el S.O. realiza lo acometido. La idea es crear un hilo adicional el cual va a ser detenido por la API mientras la aplicación funciona sin aparentar colgarse. Una vez que la búsqueda termina el hilo envía un mensaje al proceso que ha creado el hilo con la información que éste necesita.

El Timer en el ejemplo solo le dará una animación al proceso de búsqueda para que el usuario no se impaciente, ya dependerá del lector realizar la animación que vea pertinente acorde con el formato de su interfaz.


La franja horaria es GMT +2. Ahora son las 14:34:56.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi