VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "FileCopy"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

' Clipboard routines.
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" (ByVal drop_handle As Long, ByVal UINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long

' Global memory routines.
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

' DROPFILES data structure.
Private Type POINTAPI
X As Long
Y As Long
End Type

Private Type DROPFILES
pFiles As Long
pt As POINTAPI
fNC As Long
fWide As Long
End Type

' File list clipboard format code.
Private Const CF_HDROP = 15

' Global Memory Flags
Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_ZEROINIT = &H40
Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)

Public Function ClipboardSetFiles(file_names() As String) As Boolean

Dim file_string As String
Dim drop_files As DROPFILES
Dim memory_handle As Long
Dim memory_pointer As Long
Dim i As Long

' Clear the clipboard.
Clipboard.Clear

' Open the clipboard.
If OpenClipboard(0) Then
    ' Build a null-terminated list of file names.
    For i = LBound(file_names) To UBound(file_names)
        file_string = file_string & file_names(i) & vbNullChar
    Next
    file_string = file_string & vbNullChar

    ' Initialize the DROPFILES structure.
    drop_files.pFiles = Len(drop_files)
    drop_files.fWide = 0    ' ANSI characters.
    drop_files.fNC = 0      ' Client coordinates.

    ' Get global memory to hold the DROPFILES
    ' structure and the file list string.
    memory_handle = GlobalAlloc(GHND, Len(drop_files) + Len(file_string))
    If memory_handle Then
        ' Lock the memory while we initialize it.
        memory_pointer = GlobalLock(memory_handle)

        ' Copy the DROPFILES structure and the
        ' file string into the global memory.
        CopyMem ByVal memory_pointer, drop_files, Len(drop_files)
        CopyMem ByVal memory_pointer + Len(drop_files), ByVal file_string, Len(file_string)
        GlobalUnlock memory_handle

        ' Copy the data to the clipboard.
        SetClipboardData CF_HDROP, memory_handle
        ClipboardSetFiles = True
    End If

    ' Close the clipboard.
    CloseClipboard
End If
    
End Function

Public Function ClipboardGetFiles() As String()

Dim drop_handle As Long
Dim num_file_names As Long
Dim file_names() As String
Dim file_name As String * 1024
Dim i As Long

' Make sure there is file data.
If Clipboard.GetFormat(vbCFFiles) Then
    ' File data exists. Get it.
    ' Open the clipboard.
    If OpenClipboard(0) Then
        ' The clipboard is open.

        ' Get the handle to the dropped list of files.
        drop_handle = GetClipboardData(CF_HDROP)

        ' Get the number of dropped files.
        num_file_names = DragQueryFile(drop_handle, -1, vbNullString, 0)

        ' Get the file names.
        ReDim file_names(1 To num_file_names) As String
        For i = 1 To num_file_names
            ' Get the file name.
            DragQueryFile drop_handle, i - 1, file_name, Len(file_name)

            ' Truncate at the NULL character.
            file_names(i) = Left$(file_name, InStr(file_name, vbNullChar) - 1)
        Next

        ' Close the clipboard.
        CloseClipboard

        ' Assign the return value.
        ClipboardGetFiles = file_names
    End If
End If
    
End Function
