Using SetClipboardData

I have a string of file paths separated by nulls. I need to know how to use SetCliboardData to put files on the clipboard for subsequent paste operation. Would it be correct to use CF_HDROP for my data? Also how do I get a memory handle to my data?
Who is Participating?
mikeblasConnect With a Mentor Commented:

Here's some code that should get you started. It's a little rough-- specifically, it checks for errors but doesn't _react_ to errors. Also, you'll probably want to craft this code so that it doesn't get the file list from a static character array, but instead allows you to cosntruct it from some list or data structure. But, it does really work.

Anyway, here we go:

   // the static list.
   LPCTSTR pstr = "c:\\file1.txt\0c:\\file2.txt\0";
                 //123 5678901234 567 8901234567 2
                 //         11111 111 1122222222 8

   // the length of the static list. Note that we have two
   // terminating nulls and need to include that in the
   // length. The comment above shows how I counted the
   // characters. When you fix this code to work with a dynamically
   // created list of files, you'll want to write a routine to
   // count the length--strlen() (or its friends) won't work!

   int nLength = 28;

   // now, we allocate some memory to hold our list of files _and_
   // our DROPFILES structure.

   HGLOBAL hGlobal = GlobalAlloc(GMEM_SHARE, nLength + sizeof(DROPFILES));

   // if we got the memory, we'll lock it down and construct the
   // proper DROPFILES structure.
   if (hGlobal != NULL)
      DROPFILES* pDropFiles = (DROPFILES*) GlobalLock(hGlobal);
      if (pDropFiles != NULL)
         // init all structure members
         pDropFiles->pFiles = sizeof(DROPFILES);
         pDropFiles->pt.x = 0;
         pDropFiles->pt.y = 0;
         pDropFiles->fNC = FALSE;
         pDropFiles->fWide = FALSE;

         // find a pointer to the first CHAR after the structure
         // and copy our list of files there. Again, note that
         // strcpy() won't work because of the nulls in the string.
         LPSTR pFileList = LPSTR(pDropFiles) + pDropFiles->pFiles;
         memcpy(pFileList, pstr, nLength);

         // Get the clipboard
         if (OpenClipboard(NULL))
            //TODO: better error checking
            SetClipboardData(CF_HDROP, hGlobal);

      // note that we don't free the memory if the clipboard
      // operation worked!
         TRACE("Out of memory!\n");
      TRACE("Out of memory!\n");

and that's it.

Obviously, if you're not using MFC, you'll wnat to replace the TRACE() calls to with OutputDebugString(). But you really should replace them with some meaningful error recovery code.

If you've got any questions, just ask.  Also note that 50 points seems a little low when asking for sample code.

..B ekiM
 Can explain in detail the string of file paths separated by NULLs. I am not able to visualize it.
nadirkamalAuthor Commented:
It goes like this:

LPSTR string="c:\dir1\subdir1\abc.def/0c:\dir2\subdir2\ghi.jkl/0/0"

I read in MSDN that this string format can be used for using CF_HDROP clipboad format.
I just wanted to thank mikeblas so much for his solution.  Finding how to do this seemed impossible until I saw it here.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.