Click here to Skip to main content
6,594,932 members and growing! (15,617 online)
Email Password   helpLost your password?
General Reading » Hardware & System » Clipboard     Intermediate

Clipboard backup in C#

By Alessio Deiana

Backing up the clipboard using C# and Windows API calls to avoid C#'s limits on clipboard management.
C#.NET 2.0, Win2K, WinXPVS2005, Dev
Posted:25 Aug 2006
Updated:20 Sep 2006
Views:38,266
Bookmarked:33 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 4.21 Rating: 4.21 out of 5

1

2
3 votes, 30.0%
3
1 vote, 10.0%
4
6 votes, 60.0%
5

Introduction

The code uses Windows API calls to read and write into the clipboard.

It provides a ClipboardHelper, an easy to use API bridge, which offers, in addition, functions to serialize complex clipboard data to hard disk and restore them when you want, using the XmlSerializer, instead of the .NET Framework which can manage only CLR compliant data.

I tested it with Internet Explorer, Word, OpenOffice, including with image files.

Background

The .NET framework and the Windows.Forms.Clipboard class allows to manipulate only serializable data. So what happens if your clipboard contains data from a non-compliant application? Easy! The .NET Framework thinks that the clipboard is empty.

To read and manipulate these data, we need to call user32.dll and kernerl32.dll.

Using the code

The code includes two projects: the library ClipboardHelper, and the console application ClipboardTest.

The whole code is commented, to understand the API usage and the implemented saving process.

To use the library is really easy: it's a static class that exposes a static void ClipboardHelper.Serialize function to serialize the clipboard data to hard disk, and static void Deserialize to deserialize it and place it back into the clipboard, ready to be pasted.

    //Comment next line to end the demo mode

    //and backup your clipboard data.

    ClipboardHelper.Deserialize(demo);
    Console.WriteLine("restore the demo clipboard");

    //Open the clipboard and serialize into a directory

    ClipboardHelper.Serialize(fileName);
    Console.WriteLine("serialize clipboard to " + fileName);

    //Deserialize the clipboard and set data

    //to win clipboard ready to be pasted

    ClipboardHelper.Deserialize(fileName);
    Console.WriteLine("restore the clipboard " + fileName);

The test application shows an example of the ClipboardHelper in action. It deserializes the clipboard, after having copied some rows from Internet Explorer at the CodeProject homepage, backs it up, and deserializes it again, from the backup. After you run it, you have the clipboard backup in your own clipboard, ready to be pasted.

Inside the ClipboardHelper

Data returned by API calls are saved in [Serializable]DataClip objects. It allows to easily send the clipboard in a remoting context, and to save it via the XmlSerializer:

public static void SaveToFile(ReadOnlyCollection<DATACLIP> clipData, string clipName)
{
    IEnumerator<DATACLIP> cData = clipData.GetEnumerator();
    while (cData.MoveNext())
    {
        XmlSerializer xml = new XmlSerializer(typeof(DataClip));
        using (StreamWriter sw = new StreamWriter(di.FullName + 
                                 @"\" + i.ToString() + ".cli",false))
        {
            xml.Serialize(sw, cData.Current);
        }
    }
 }

Deep into the clipboard

The clipboard contains data in fragmented buffers. Each of them has a specific DataFormat that is used by the clipboard viewer and the software from which data has been copied, to recognize how to correctly render the buffer.

The function EnumClipboardFormats of user32.dll returns the array of DataFormats contained in the clipboard. For each DataFormat, we can call GetClipboardData. It returns the handle to the clipboard object. By calling GlobalSize and GlobalLock from kernel32.dll, we obtain the clipboard object properties (its length and the pointer to it), so we can copy the clipboard object into our buffer (a byte array) via the Marshal.Copy method.

To get the clipboard, first of all, check to open the clipboard:

Win32ClipboardAPI.OpenClipboard(IntPtr.Zero)

Then, we get all DataFormats; querying them we get the clipboard data buffer:

    //Init a list of ClipData,

    // which will contain each Clipboard Data

    List<DataClip> clipData = new List<DataClip>();
    //Loop for each clipboard data type

    uint format = 0;
    while ((format = Win32ClipboardAPI.EnumClipboardFormats(format)) != 0)
    {
        //Get the formatName

        StringBuilder formatName = new StringBuilder();
        Win32ClipboardAPI.GetClipboardFormatName(format, 
                                       formatName, 100);
        
        //Get the pointer for the current Clipboard Data 

        IntPtr pos = Win32ClipboardAPI.GetClipboardData(format);
        //Get the clipboard buffer data properties

        UIntPtr lenght = Win32MemoryAPI.GlobalSize(pos);
        IntPtr gLock = Win32MemoryAPI.GlobalLock(pos);
        byte[] buffer;
        //Init a buffer which will contain the clipboard data

        buffer = new byte[(int)lenght];
        int l = Convert.ToInt32(lenght.ToString());
        //Copy data from clipboard to our byte[] buffer

        Marshal.Copy(gLock, buffer, 0, l);
        //Create a ClipData object that 

        //represents the current clipboard data

        DataClip cd = new DataClip(format, 
                      formatName.ToString(), buffer);
        //Add current Clipboard Data to the list

        clipData.Add(cd);
}

History

  • 08/23/2006 - Initial release.
  • 08/29/2006 - The whole saving system has been changed, implementing the XmlSerializer. It works better, solving the bugs in the writing file process.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Alessio Deiana


Member
25 years old, began coding from basic, since 1992.

Interested in coding database management windows and web application, in C#, Xml, Sql, Asp.Net

Soon will be a dentist, but yet doesn't not know if will use hands or robots to cure the teeth.

Actually own and work for Italian softwarehouse Vigo s.r.l.: www.vigoline.it

Occupation: Web Developer
Location: Italy Italy

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralOutlook problem Pinmembervice9:44 12 Feb '07  
GeneralClipboard application sometime working Pinmembernip909:38 14 Oct '06  
GeneralRe: Clipboard application sometime working PinmemberAlessio Deiana0:10 17 Oct '06  
GeneralRe: Clipboard application sometime working Pinmembernip900:43 20 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Sep 2006
Editor: Smitha Vijayan
Copyright 2006 by Alessio Deiana
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project