Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. COMException when getting custom data from system clipboard [modified]

COMException when getting custom data from system clipboard [modified]

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharphelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike_Finch
    wrote on last edited by
    #1

    I have a custom type, FooNode, defined in my C# code.   I want to add an instance of that custom type to the global System.Windows.Forms.Clipboard, and then retrieve it from the clipboard again.   The add seems to work, but I am not able to retrieve the instance.   Upon retrieval, several exceptions print to standard output like the following:

    A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Windows.Forms.dll
    (... and 9 more just like above)

    The result of the retrieval is a null reference.   There is no crash or halt.   The above exceptions are being dealt with internally;   I am not able to catch them.   The problem is not with the DataObject itself, because I can retrieve my FooNode from it.   I just can't retreive my FooNode from the clipboard's DataObject. I am able to add and then retreive other types of objects to the system clipboard, such as strings and System.Guid.   Why can I not retrieve an object of my custom type? Following is my test code.   Call FooTest.Test() to run.

    using System;
    using System.Diagnostics;
    using System.Windows.Forms;

    public class FooNode
    {
    private Guid m_Guid;
    private string m_Name = String.Empty;

    public FooNode( )
    {
        m\_Guid = Guid.NewGuid();
        m\_Name = "Foo";
    }
    
    public Guid Guid
    {
        get { return m\_Guid; }
        set { m\_Guid = value; }
    }
    
    public string Name
    {
        get { return m\_Name; }
        set { m\_Name = value; }
    }
    

    }

    public class FooTest
    {
    // Entry point for test of using system clipboard.
    public static void Test( )
    {
    FooNode fooNode = new FooNode();

        // Add a FooNode to the system clipboard.    
        DataObject dob = new DataObject( fooNode );
        dob.SetData( typeof( Guid ), fooNode.Guid );
        dob.SetData( DataFormats.StringFormat, fooNode.Guid.ToString() );
        Clipboard.SetDataObject( dob );
    
        // Retrieve the FooNode from the system clipboard.
        // \*\*\* Notice that the returned object is null. \*\*\*
        object raw = Clipboard.GetDataObject().GetData( typeof( FooNode ) );
    
        // This spam function demonstrates what can and cannot be retrieved from the clipboard.
        Spam( Clipboard.GetDataObject(), new Type\[\] { typeof( FooNode ), typeof( Guid ) } );
    }
    
    
    public static void Spam( IDataObje
    
    L 1 Reply Last reply
    0
    • M Mike_Finch

      I have a custom type, FooNode, defined in my C# code.   I want to add an instance of that custom type to the global System.Windows.Forms.Clipboard, and then retrieve it from the clipboard again.   The add seems to work, but I am not able to retrieve the instance.   Upon retrieval, several exceptions print to standard output like the following:

      A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Windows.Forms.dll
      (... and 9 more just like above)

      The result of the retrieval is a null reference.   There is no crash or halt.   The above exceptions are being dealt with internally;   I am not able to catch them.   The problem is not with the DataObject itself, because I can retrieve my FooNode from it.   I just can't retreive my FooNode from the clipboard's DataObject. I am able to add and then retreive other types of objects to the system clipboard, such as strings and System.Guid.   Why can I not retrieve an object of my custom type? Following is my test code.   Call FooTest.Test() to run.

      using System;
      using System.Diagnostics;
      using System.Windows.Forms;

      public class FooNode
      {
      private Guid m_Guid;
      private string m_Name = String.Empty;

      public FooNode( )
      {
          m\_Guid = Guid.NewGuid();
          m\_Name = "Foo";
      }
      
      public Guid Guid
      {
          get { return m\_Guid; }
          set { m\_Guid = value; }
      }
      
      public string Name
      {
          get { return m\_Name; }
          set { m\_Name = value; }
      }
      

      }

      public class FooTest
      {
      // Entry point for test of using system clipboard.
      public static void Test( )
      {
      FooNode fooNode = new FooNode();

          // Add a FooNode to the system clipboard.    
          DataObject dob = new DataObject( fooNode );
          dob.SetData( typeof( Guid ), fooNode.Guid );
          dob.SetData( DataFormats.StringFormat, fooNode.Guid.ToString() );
          Clipboard.SetDataObject( dob );
      
          // Retrieve the FooNode from the system clipboard.
          // \*\*\* Notice that the returned object is null. \*\*\*
          object raw = Clipboard.GetDataObject().GetData( typeof( FooNode ) );
      
          // This spam function demonstrates what can and cannot be retrieved from the clipboard.
          Spam( Clipboard.GetDataObject(), new Type\[\] { typeof( FooNode ), typeof( Guid ) } );
      }
      
      
      public static void Spam( IDataObje
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I'm no expert in this matter, but I doubt your object makes it to the Clipboard. I have a hunch you need a [Serializable] for your FooNode class. Also you may want to add ",true" to Clipboard.SetDataObject() to make your object survive your app. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      M 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I'm no expert in this matter, but I doubt your object makes it to the Clipboard. I have a hunch you need a [Serializable] for your FooNode class. Also you may want to add ",true" to Clipboard.SetDataObject() to make your object survive your app. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        M Offline
        M Offline
        Mike_Finch
        wrote on last edited by
        #3

        Awesome! Decorating my FooNode class with the SerializableAttribute got it working. Thank you.

        [Serializable]
        public class FooNode
        {
        ...
        }

        L 1 Reply Last reply
        0
        • M Mike_Finch

          Awesome! Decorating my FooNode class with the SerializableAttribute got it working. Thank you.

          [Serializable]
          public class FooNode
          {
          ...
          }

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          you're welcome. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups