How to paste files from Windows Explorer into your application #161

Question
I would like to be able to go to Windows Explorer, select a series of files, and then allow the user to "Paste" these files into my application. I simply need a list of the file names that were copied to the clipboard. Anyone know how to access this list?

You can use OLE Drag & Drop, but since Explorer creates a standard CF_HDROP clipboard block you can also hack it this way:

uses
  clipbrd, shellapi;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  f: THandle;
  buffer: Array [0..MAX_PATH] of Char;
  i, numFiles: Integer;
begin
  Clipboard.Open;
  try
    f := Clipboard.GetAsHandle(CF_HDROP);
    if f <> 0 then
    begin
      numFiles := DragQueryFile(f, $FFFFFFFF, nil, 0);
      memo1.Clear;
      for i:= 0 to numfiles - 1 do
      begin
        buffer[0] := #0;
        DragQueryFile( f, i, buffer, sizeof(buffer));
        memo1.lines.add(buffer);
      end;
    end;
  finally
    Clipboard.close;
  end;
end;
Original resource: The Delphi Pool
Author: Peter Below
Added: 2010/06/02
Last updated: 2010/06/02