What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleGet and set file names copied to the clipboard by Windows Explorer
KeywordsWindows Explorer, clipboard, files, file names
CategoriesFiles and Directories, Utilities, Tips and Tricks, Windows
Manipulating the clipboard with API functions uses drap-and-drop capabilities.

To get the file names on the clipboard, first use Clipboard.GetFormat(vbCFFiles) to see if there are file names in the clipboard. If there are, use the OpenClipboard API function to open the clipboard for use by the API. Then use DragQueryFile to get the number of file names and their values. Close the clipboard with CloseClipboard.

' Get an array of the files listed in the clipboard.
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)

            ' Close the clipboard.

            ' Assign the return value.
            ClipboardGetFiles = file_names
        End If
    End If
End Function
To set the file names on the clipboard, first clear the clipboard using Clipboard.Clear. Then open the clipboard for API use with OpenClipboard. Next build a string containing the file names. Put a vbNullChar between the names and end the string with two vbNullChar characters.

Now initialize a DROPFILES structure and put it in global memory. Set the pFiles field to the size of the DROPFILES structure. This gives the offset from the beginning of the structure to the location of the file name string. Use GlobalAlloc to get a chunk of global memory big enough to hold the structure and the string. Use GlobalLock to lock the memory and get a pointer to it. Then use CopyMem to copy the DROPFILES structure and the file name string into the memory. Now unlock the memory and use it to set the clipboard's data with the SetClipboardData API function. Finally, close the clipboard with CloseClipboard.

Simple, right?

' Copy the file names into the clipboard.
' Return True if we succeed.
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.

    ' 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) & _
        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) + _
        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, _
            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.
    End If
End Function
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.