File to Clipboard

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

path[len+1]='/0';
path[len+2]='/0';
len=len+2;
LPDATAOBJECT pIDataObject=(LPDATAOBJECT) malloc(1);
FORMATETC Formatetc={CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
STGMEDIUM Medium;
Medium.tymed=TYMED_HGLOBAL;
HGLOBAL hMem=Medium.hGlobal;
DWORD dwLen=len;
LPDROPFILES pDropFiles=(LPDROPFILES)GlobalAlloc(GPTR, dwLen);
pDropFiles->pFiles=dwLen;
pDropFiles->fWide=0;
pDropFiles=(LPDROPFILES)path;
pIDataObject->SetData(&Formatetc, &Medium, TRUE);
OleSetClipboard(pIDataObject);



nadirkamalAsked:
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.

Paul SinnemaCommented:
Is this the actual code? If it is, you should change the code:

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

tp

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

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

Hope this helps

0
mikeblasCommented:
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
0
nadirkamalAuthor Commented:
Paul
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.
Thanks!
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

mikeblasCommented:

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);
         GlobalUnlock(hGlobal);

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

      // note that we don't free the memory if the clipboard
      // operation worked!
      }
      else
      {
         TRACE("Out of memory!\n");
         GlobalFree(hGlobal);
      }
   }
   else
      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
0

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
nadirkamalAuthor Commented:
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!
0
mikeblasCommented:
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
0
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.