Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Pitfalls when using COleDataSource for clipboard and Drag & Drop operations

, 21 Feb 2012
Rate this:
Please Sign up or sign in to vote.
Information about COleDataSource not contained in the Microsoft documentation or hardly to be found.

Pitfall 1: OLE initialization

Any function using OLE may not work or show unexpected behaviour when OLE is not initialized. So don't forget to call AfxOleInit() from InitInstance().

Pitfall 2: COleDataSource allocation

When using SetClipboard(), COleDataSource objects must be allocated on the heap and not on the stack. Looking at the MFC sources, you will find a call to InternalRelease() at the end of the SetClipboard() function. InternalRelease() is implemented in the CCmdTarget base class and is finally using delete this to destroy the object. Therefore, the COleDataSource object must be allocated on the heap and the object is no longer valid upon return from SetClipboard().

Pitfall 3: Memory leaks when using DoDragDrop()

Contrary to SetClipboard(), the COleDataSource object is not destroyed automatically. So this must be done in your own code using InternalRelease() or ExternalRelease() (recommended, calls InternalRelease() if not aggregated). Don't use delete.

Pitfall 4: Using data rendering with the clipboard

When using data rendering, data are only copied to allocated memory or file when used. This is especially useful with large amounts of data and when providing multiple clipboard formats. But with clipboard operations, there are two drawbacks:

  1. Nothing is copied when the source application or window has been closed meanwhile.
  2. Copies those items that are selected when the insert operation occurs, not those that were selected when the SetClipboard() command has been called.

Pitfall 5: Drag&Drop may not work when applications are executed by different users

This applies to Windows Vista and later with enabled UAC. When the target application is executed with higher privileges than the source application, Drag&Drop is not supported (the cursor does not change to indicate the possibility of moving or copying). In the other direction, the cursor indicates that Drag&Drop is possible, and when dropping the move or copy effect is returned by DoDragDrop(), but nothing is dropped. See also this link [^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jochen Arndt
Engineer
Germany Germany
Jochen started programming in the 80s using Assembler (Z80, 8080, x86), BASIC, C, and TurboPascal. Actually he uses mainly C++ and Perl. He is a graduate engineer in communications engineering (University of Applied Sciences Kiel, 1991).
Follow on   Google+

Comments and Discussions

 
QuestionVictim of Pitfall 3 PinmemberLyverbe28-Jun-13 4:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 21 Feb 2012
Article Copyright 2012 by Jochen Arndt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid