Visual Basic System Services
DragQueryFile: Identify Files Copied to the Clipboard
Posted:   Sunday January 15, 2006
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   VBnet - Randy Birch


EnumClipboardFormats: Identify Clipboard Data Available

This demo shows one way to determine the complete path/filename of files a user has selected in Explorer to copy or cut.

The code first tests to determine the type of data on the clipboard. If the format CF_HDROP is available, the clipboard is opened and GetClipboardData is called. Next DragQueryFile is called to determine the number of entries, then twice more for each entry to get the string buffer size and the path and filename.

To test this demo after creating the code below, open Explorer and select any file(s) and do a Copy. Then run the demo to see the selected filenames listed. Now try pasting into notepad -- nothing will happen because the clipboard data is not in text format.

Grabbing this info from the clipboard does not affect the files on the disk in any way,

 BAS Module Code

 Form Code
Add a command button (Command1) and a list box (List1) to a form, along with the following code;

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Const CF_HDROP As Long = 15

Private Declare Function IsClipboardFormatAvailable Lib "user32" _
  (ByVal uFormat As Long) As Long
Private Declare Function OpenClipboard Lib "user32" _
  (ByVal hWndNewOwner As Long) As Long

Private Declare Function GetClipboardData Lib "user32" _
  (ByVal uFormat As Long) As Long

Private Declare Sub CloseClipboard Lib "user32" ()

Private Declare Function DragQueryFile Lib "shell32.dll" _
   Alias "DragQueryFileA" _
  (ByVal hDrop As Long, _
   ByVal iFile As Long, _
   ByVal lpszFile As String, _
   ByVal cch As Long) As Long

Private Declare Function lstrlenW Lib "kernel32" _
  (ByVal lpString As Long) As Long

Private Sub Form_Load()

   With List1
      .Move 200, 200, 5000, 2400
   End With

   With Command1
      .Move 200, List1.Top + List1.Height + 200, 2000, 345
      .Caption = "Get Clipboard File List"
   End With
End Sub

Private Sub Command1_Click()

   Call ClipboardGetFiles

End Sub

Private Function ClipboardHasFiles() As Boolean

  'CF_HDROP indicates that the clipboard
  'identifies a list of files.
   ClipboardHasFiles = (IsClipboardFormatAvailable(CF_HDROP) > 0)

End Function

Private Sub ClipboardGetFiles()

   Dim hCBData As Long
   Dim numFiles As Long
   Dim cnt As Long
   Dim cbBuff As Long
   Dim buff As String

   If ClipboardHasFiles() Then
      If OpenClipboard(0&) <> 0 Then
        'GetClipboardData returns a handle
        'that identifies a list of files.
        'The application retrieves this
        'information by passing the handle
        'to the DragQueryFile function.
         hCBData = GetClipboardData(CF_HDROP)
         If hCBData <> 0 Then
           'the first call to DragQueryFile
           'passes -1 as the iFile value; the
           'api in return indicates the number
           'of files to be dropped.
           'note that once we're done we do not call
           'DragFinish as we did nothing with the
           'data other than list it.
            numFiles = DragQueryFile(hCBData, -1, 0&, 0&)
           'loop for each file ..
            For cnt = 0 To numFiles - 1
              '.. and each subsequent call to DragQueryFile
              'returns info about the file specified by
              'the iFile value. By passing null for the
              'buffer and size, the required buffer size
              'is returned.
               cbBuff = DragQueryFile(hCBData, cnt, vbNullString, 0&)

               If cbBuff > 0 Then
                 'pad the buffer adding one for 
                 'the terminating null char
                  buff = Space$(cbBuff + 1)
                  cbBuff = Len(buff)
                 'the final call will return the file;
                 'the return value, not used here, is
                 'the size of the file string returned.
                  If DragQueryFile(hCBData, cnt, buff, cbBuff) > 0 Then
                    'just add to list for this demo
                     List1.AddItem TrimNull(buff)
                  End If
               End If
            Next cnt

         End If  'hCbData
         Call CloseClipboard
      End If  'OpenClipboard
   End If  'ClipboardHasFiles
End Sub

Private Function TrimNull(startstr As String) As String

   TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
End Function


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter