File to Clipboard

Posted on 2000-01-05
Medium Priority
Last Modified: 2013-12-03
I want to place a file on the clipboard for subsequent paste action anywhere in windows explorer. Here's my code that doesn't seem to work. Can any expert please tell me what I am doing wrong.

Say the file is C:\temp\file1.txt

LPSTR path="c:\temp\file1.txt";
int len=strlen(path);
// Add two nulls at end of character array to support CF_HDROP format

HGLOBAL hMem=Medium.hGlobal;
DWORD dwLen=len;
pIDataObject->SetData(&Formatetc, &Medium, TRUE);

Question by:nadirkamal
  • 3
  • 2

Expert Comment

by:Paul Sinnema
ID: 2330495
Is this the actual code? If it is, you should change the code:

LPSTR path="c:\temp\file1.txt";


LPSTR path="c:\\temp\\file1.txt";

a '\t' for instance is translated to a TAB-character.

Hope this helps

LVL 11

Expert Comment

ID: 2333642
You're not setting up the structure in the right way at all. (It's amazing you don't cause a crash!) That malloc(1) trick is nonsense.

..B ekiM

Author Comment

ID: 2336562
Actually the first line is not my actual code. I am being returned the file path from somewhere else.

B ekim
Could you please correct my code for the structure.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

LVL 11

Accepted Solution

mikeblas earned 40 total points
ID: 2339048

There's dozens of things wrong with the code you provided in your question. I don't normally write sample code for such an insultingly low amount of points, but it really looks like you need help badly.  (15 points is normally too low for me to even consider answering!)

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.

Note that I give the data directly to the clipboard and don't fool with OLE. Since you're just calling OleSetClipboard(), you don't need to use OLE. If you were providing this data via an IDataObject that someone else gave you, then you'd need to use the OLE formats (specifically, the TYMED and FORMATETC).

If you've got any questions, just ask.  

..B ekiM

Author Comment

ID: 2342114
Thanks a lot Mike. Your code really made my day!
I apologize for the low points. I have already accepted your answer in my other question worth 50 points. If you like you may lock yet another question of mine on the same topic (!!) at http://www.experts-exchange.com/jsp/qShow.jsp?ta=winprog&qid=10253545   
I hope this makes up for the low points here.
Thanks again!
LVL 11

Expert Comment

ID: 2345998
A total of 95 points seems much more fair.  Thanks for offering that--that's very honest of you. I'll go lock the other question.

..B ekiM

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
To export Lotus Notes to Outlook PST or Exchange and Domino Server files to Exchange Server or PST files with ease, go for Kernel for Lotus Notes to Outlook conversion tool. Through the video, you can watch the conversion process. A common user with…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question