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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 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.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I just wanted to thank mikeblas so much for his solution.  Finding how to do this seemed impossible until I saw it here.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.