Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 317317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:27:53+00:00 2026-05-12T08:27:53+00:00

I’m trying to drag data from the Winforms portion of my application on a

  • 0

I’m trying to drag data from the Winforms portion of my application on a WPF controls that’s contained inside an “ElementHost”. And it crashes when I try doing so.

Trying the same thing but from Winforms to Winforms works fine. (See example code below)

I need help on making this work… have any clues what I’m doing wrong?

Thanks!


Example:
In the sample code below, I’m just trying to drag a custom MyContainerClass object created when initating the drag on the label control on a 1) System.Windows.Forms.TextBox (Winforms) and 2) System.Windows.TextBox (WPF, added to an ElementHost).

Case 1) works fine but case 2) is crashing when trying to retrieve the drop data using GetData(). GetDataPresent(“WindowsFormsApplication1.MyContainerClass”) returns “true” so In theory, I should be able to retrive my drop data of that type like in Winforms.

Here is the stack trace of the crash:

"Error HRESULT E_FAIL has been returned from a call to a COM component" with the following stack trace:
 at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
 at System.Windows.Forms.DataObject.GetDataIntoOleStructs(FORMATETC& formatetc, STGMEDIUM& medium)
 at System.Windows.Forms.DataObject.System.Runtime.InteropServices.ComTypes.IDataObject.GetDataHere(FORMATETC& formatetc, STGMEDIUM& medium)
 at System.Windows.Forms.DataObject.System.Runtime.InteropServices.ComTypes.IDataObject.GetData(FORMATETC& formatetc, STGMEDIUM& medium)
 at System.Windows.DataObject.OleConverter.GetDataInner(FORMATETC& formatetc, STGMEDIUM& medium)
 at System.Windows.DataObject.OleConverter.GetDataFromOleHGLOBAL(String format, DVASPECT aspect, Int32 index)
 at System.Windows.DataObject.OleConverter.GetDataFromBoundOleDataObject(String format, DVASPECT aspect, Int32 index)
 at System.Windows.DataObject.OleConverter.GetData(String format, Boolean autoConvert, DVASPECT aspect, Int32 index)
 at System.Windows.DataObject.OleConverter.GetData(String format, Boolean autoConvert)
 at System.Windows.DataObject.GetData(String format, Boolean autoConvert)
 at System.Windows.DataObject.GetData(String format)
 at WindowsFormsApplication1.Form1.textBox_PreviewDragEnter(Object sender, DragEventArgs e) in WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 48  

Here is some code:

// -- Add an ElementHost to your form --
// -- Add a label to your form --

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        System.Windows.Controls.TextBox textBox = new System.Windows.Controls.TextBox();
        textBox.Text = "WPF TextBox";
        textBox.AllowDrop = true;
        elementHost2.Child = textBox;
        textBox.PreviewDragEnter += new System.Windows.DragEventHandler(textBox_PreviewDragEnter);

        System.Windows.Forms.TextBox wfTextBox = new System.Windows.Forms.TextBox();
        wfTextBox.Text = "Winforms TextBox";
        wfTextBox.AllowDrop = true;
        wfTextBox.DragEnter += new DragEventHandler(wfTextBox_DragEnter);
        Controls.Add(wfTextBox);
    }

    void wfTextBox_DragEnter(object sender, DragEventArgs e)
    {
        bool dataPresent = e.Data.GetDataPresent("WindowsFormsApplication1.MyContainerClass");

        // NO CRASH here!
        object data = e.Data.GetData("WindowsFormsApplication1.MyContainerClass");
    }

    void textBox_PreviewDragEnter(object sender, System.Windows.DragEventArgs e)
    {
        bool dataPresent = e.Data.GetDataPresent("WindowsFormsApplication1.MyContainerClass");

        // Crash appens here!!
        // {"Error HRESULT E_FAIL has been returned from a call to a COM component."}
        object data = e.Data.GetData("WindowsFormsApplication1.MyContainerClass");
    }

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        label1.DoDragDrop(new MyContainerClass(label1.Text), DragDropEffects.Copy);
    }
}

public class MyContainerClass
{
    public object Data { get; set; }

    public MyContainerClass(object data)
    {
        Data = data;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:27:53+00:00Added an answer on May 12, 2026 at 8:27 am

    @Pedery & jmayor: Thanks for the suggestions guys! (see my findings below)

    After quite a few experimentation, trials and errors, and a bit of “Reflector’ing”, I managed to figure out exactly why I was receiving the cryptic error message “Error HRESULT E_FAIL has been returned from a call to a COM component”.

    It was due to the fact that when dragging data WPF <-> Winforms in a same app, that data has to be Serializable!

    I’ve checked how difficult it would be to transform all of our classes to “Serializable” and I would have a been a real pain for a couple of reasons… one, we would need to practically make all of classes serializable and two, some of these classes have references to Controls! And Controls aren’t serializable. So a major refactoring would have been needed.

    So… since we wanted to pass any object of any class to drag from/to WPF inside the same application, I decided to create a wrapper class, with the Serializable attribute and implementing ISerializable. I would have 1 contructor with 1 parameter of type “object” which would be the actual drag data. That wrapper, when serializing/de-serializing, would serialize not the object itself… but rather the IntPtr to the object (which we can do since we only want that functionnality inside our 1 instance only application.) See code sample below:

    [Serializable]
    public class DataContainer : ISerializable
    {
    public object Data { get; set; }
    
    public DataContainer(object data)
    {
        Data = data;
    }
    
    // Deserialization constructor
    protected DataContainer(SerializationInfo info, StreamingContext context)
    {
        IntPtr address = (IntPtr)info.GetValue("dataAddress", typeof(IntPtr));
        GCHandle handle = GCHandle.FromIntPtr(address);
        Data = handle.Target;
        handle.Free();
    }
    
    #region ISerializable Members
    
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        GCHandle handle = GCHandle.Alloc(Data);
        IntPtr address = GCHandle.ToIntPtr(handle);
        info.AddValue("dataAddress", address);
    }
    
    #endregion
    }
    

    To keep the IDataObject functionnality, I created the following DataObject wrapper:

    public class DataObject : IDataObject
    {
    System.Collections.Hashtable _Data = new System.Collections.Hashtable();
    
    public DataObject() { }
    
    public DataObject(object data)
    {
        SetData(data);
    }
    
    public DataObject(string format, object data)
    {
        SetData(format, data);
    }
    
    #region IDataObject Members
    
    public object GetData(Type format)
    {
        return _Data[format.FullName];
    }
    
    public bool GetDataPresent(Type format)
    {
        return _Data.ContainsKey(format.FullName);
    }
    
    public string[] GetFormats()
    {
        string[] strArray = new string[_Data.Keys.Count];
        _Data.Keys.CopyTo(strArray, 0);
        return strArray;
    }
    
    public string[] GetFormats(bool autoConvert)
    {
        return GetFormats();
    }
    
    private void SetData(object data, string format)
    {
        object obj = new DataContainer(data);
    
        if (string.IsNullOrEmpty(format))
        {
            // Create a dummy DataObject object to retrieve all possible formats.
            // Ex.: For a System.String type, GetFormats returns 3 formats:
            // "System.String", "UnicodeText" and "Text"
            System.Windows.Forms.DataObject dataObject = new System.Windows.Forms.DataObject(data);
            foreach (string fmt in dataObject.GetFormats())
            {
                _Data[fmt] = obj;
            }
        }
        else
        {
            _Data[format] = obj;
        }
    }
    
    public void SetData(object data)
    {
        SetData(data, null);
    }
    
    #endregion
    }
    

    And we are using the above classes like this:

    myControl.DoDragDrop(new MyNamespace.DataObject(myNonSerializableObject));
    
    // in the drop event for example
    e.Data.GetData(typeof(myNonSerializableClass));
    

    I know I know… it’s not very pretty… but it’s doing what we wanted. We also created a dragdrop helper class which masks the DataObject creation and has templated GetData functions to retrieve the data without any cast… a bit like:

    myNonSerializableClass newObj = DragDropHelper.GetData<myNonSerializableClass>(e.Data);
    

    So thanks again for the replies! You guys gave me good ideas where to look at for possible solutions!

    -Oli

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.