Link to home
Start Free TrialLog in
Avatar of nadirkamal
nadirkamal

asked on

Question on Clipboard

Easy 30 points for you experts :
I need to transfer a file to the clipboard so that it can be subsequently pasted in windows explorer. Can someone give me the code (in C++ / WIN32 API) that I can insert in my program preferably without getting into COM and OLE.

Avatar of jhance
jhance

No.  

This cannot be done without using the Windows Shell functions.  These are COM by definition.  You can copy text and other objects to the Windows clipboard without COM but the ability to copy/paste a file into a folder is a Windows Shell function.
Avatar of nadirkamal

ASKER

Can you give me a sample code using the shell functions.  
For 30pts. you want a complete example?  Maybe some other expert feels generous today.  Here's a description of how to do it from the SDK Docs...


Clipboard Data Transfers
The Clipboard is the simplest way to transfer shell data. The basic procedure is similar to standard Clipboard data transfers. However, because you are transferring a pointer to a data object, not the data itself, you must use the OLE clipboard API instead of the standard clipboard API. The following procedure outlines how to use the OLE clipboard API to transfer shell data with the Clipboard:

The data source creates a data object to contain the data.
The data source calls OleSetClipboard, which places a pointer to the data object's IDataObject interface on the Clipboard.
The target calls OleGetClipboard to retrieve the pointer to the data object's IDataObject interface.
The target extracts the data by calling the IDataObject::GetData method.
With some shell data transfers, the target might also need to call the data object's IDataObject::SetData method to provide feedback to the data object on the outcome of the data transfer. See Handling Optimized Move Operations for an example of this type of operation.
Drag-Drop
//(write to the clipboard)
// Say the file's text is in mystring:
char mystring = ??;
int nChars = strlen(mystring) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, nChars);
HGLOBAL ptr = GlobalLock(hMem);
memcpy(ptr, mystring, nChars);
GlobalUnlock(hMem);
::OpenClipboard(NULL);
::EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
CloseClipboard();

// TO paste:
::PostMessage(EditHandle, WM_PASTE, 0, 0);

//where EditHandle is the handle of the window.
Sorry, you can reject, I didn't notice you wanted to copy and paste a file rather than text of a file
mjswart
Thanks for the code anyway. Please see if you can answer the same question that is rephrased under "File to Clipboard" with my code that doesn't work. If you do you get your 30 points here as well !
ASKER CERTIFIED SOLUTION
Avatar of mikeblas
mikeblas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial