Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a DoDragDrop where I set the data to a Point.when I drag within one instance – everything's OK. But when I drag between two instances of the program Visual Studio gives me this error:

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

Why?

EDIT: here's the code:

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

And:

Point e2 = (Point)e.Data.GetData("ThePoint");
like image 776
ispiro Avatar asked Jun 11 '12 12:06

ispiro


1 Answers

The specified record cannot be mapped

Note the oddity of the word "record". It is a COM-centric word for "struct". What you are trying to do almost works, but not quite. The DoDragDrop() method properly marshals the Point structure to a COM object, possible because Point has the [ComVisible(true)] attribute. The missing ingredient is the info required by IRecordInfo, a COM interface that describes the layout of the structure. Required because structures have a very compiler dependent layout.

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

Everything good, except for the part where the receiver of the COM object has to translate it back to a managed object, a Point. That requires finding out what type library contains the object definition so IRecordInfo can do its job. Which is recorded in the registry, HKCR\Record key. Which does not contain an entry for Point. Kaboom.

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

like image 194
Hans Passant Avatar answered Sep 23 '22 16:09

Hans Passant