views:

374

answers:

2

Hi,

I'm trying to copy a custom object from a RDC window into host (my local) machine. It fails. Here's the code that i'm using to 1) copy and 2) paste:

1) Remote (client running on Windows XP accessed via RDC):

            //copy entry
            IDataObject ido = new DataObject();
            XmlSerializer x = new XmlSerializer(typeof(EntryForClipboard));
            StringWriter sw = new StringWriter();
            x.Serialize(sw, new EntryForClipboard(entry));
            ido.SetData(typeof(EntryForClipboard).FullName, sw.ToString());
            Clipboard.SetDataObject(ido, true);

2) Local (client running on local Windows XP x64 workstation):

                //paste entry
                IDataObject ido = Clipboard.GetDataObject();
                DataFormats.Format cdf = DataFormats.GetFormat(typeof(EntryForClipboard).FullName);

                if (ido.GetDataPresent(cdf.Name)) //<- this always returns false
                {
                    //can never get here!
                    XmlSerializer x = new XmlSerializer(typeof(EntryForClipboard));
                    string xml = (string)ido.GetData(cdf.Name);
                    StringReader sr = new StringReader(xml);
                    EntryForClipboard data = (EntryForClipboard)x.Deserialize(sr);
                }

It works perfectly on the same machine though.

Any hints?

A: 

There are a couple of things you could look into:

  1. Are you sure the serialization of the object truely converts it into XML? Perhaps the outputted XML have references to your memory space? Try looking at the text of the XML to see.
  2. If you really have a serialized XML version of the object, why not store the value as plain-vanilla text and not using typeof(EntryForClipboard) ? Something like:

    XmlSerializer x = new XmlSerializer(typeof(EntryForClipboard));
    StringWriter sw = new StringWriter();
    x.Serialize(sw, new EntryForClipboard(entry));
    Clipboard.SetText(sw.ToString(), TextDataFormat.UnicodeText);
    

    And then, all you'd have to do in the client-program is check if the text in the clipboard can be de-serialized back into your object.

scraimer
A: 

Ok, found what the issue was. Custom format names get truncated to 16 characters when copying over RDC using custom format. In the line

ido.SetData(typeof(EntryForClipboard).FullName, sw.ToString());

the format name was quite long.

When i was receiving the copied data on the host machine the formats available had my custom format, but truncated to 16 characters.

IDataObject ido = Clipboard.GetDataObject();
ido.GetFormats(); //used to see available formats.

So i just used a shorter format name:

//to copy
ido.SetData("MyFormat", sw.ToString());
...
//to paste
DataFormats.Format cdf = DataFormats.GetFormat("MyFormat");
if (ido.GetDataPresent(cdf.Name)) {
  //this not works
  ...
Muxa