Why can't I drag a point between two instances of the program?

I have a DoDragDrop where I set the data to Point . When I drag in one instance - everything is OK. But when I drag between two instances of Visual Studio, I get this error:

The specified record cannot be mapped to a managed value class.

Why?

EDIT: here is the code:

 DataObject d = new DataObject(); d.SetData("ThePoint", MyPoint); DragDropEffects e = DoDragDrop(d, DragDropEffects.Move); 

and

 Point e2 = (Point)e.Data.GetData("ThePoint"); 
+4
source share
2 answers

The specified record cannot be displayed.

Note the strangeness of the word "record." This is a COM-oriented word for "struct". What you are trying to do almost works, but not quite. The DoDragDrop () method correctly arranges the Point structure on a COM object, possibly because Point has the [ComVisible (true)] attribute. The missing component is the information required by IRecordInfo, a COM interface that describes the layout of the structure. Required because structures have a very compiler-specific layout.

This interface is usually implemented by reading the structure definition from the type library. In fact, it is available, the Point structure is described in c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ system.drawing.tlb. You can look at it using the OleView.exe tool, File + View Typelib.

Everything is fine, except for the part where the recipient of the COM object must translate it back to the managed object, Point. This requires finding out which type library contains the object definition, so IRecordInfo can do its job. What is written to the registry, HKCR \ Record. Which does not contain an entry for Point. Kaboom.

Create your own class (not struct) for storing data, give it the [Serializable] attribute so that it can be trivially marshaled.

+3
source

It will look like a hack, but you can do it, I tested it, it works. Edit Guess he is not responding. WHY? question.

 private void panel1_MouseDown(object sender, MouseEventArgs e) { Point MyPoint = new Point(100, 200); DoDragDrop(new string[] { MyPoint.X.ToString(), MyPoint.Y.ToString() }, DragDropEffects.Copy); } private void Form1_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } private void Form1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(string[]))) { string[] item = (string[])e.Data.GetData(typeof(string[])); Point e2 = new Point(Int32.Parse(item[0]), Int32.Parse(item[1])); MessageBox.Show(e2.X+":"+e2.Y); } } 
+1
source

All Articles