Thursday, January 20, 2005

Drag & Drop To Windows Forms Control Hosted In Internet Explorer

We have a windows forms control that we host in a browser that we needed to make a drop target. This control is granted fulltrust already based on our strong name.

Based on this we thought this setup would be trivial.  Well we ran into two issues I want to get in the google index for others that hit them.

1. In our _DragDrop handler we only received the name of the first file that was dropped even with a multi selected drop.  Come to find out this is because the framework after grabbing the first file name does a FileIOPermission Demand assuming we are going to eventually do something with the file.  In our case however we just need the filename.  The Demand fails and the framework silently catches it returning only a single filename.  This has been reported as a bug.  The fix?  Make sure that you assert FileIOPermission on your _DragDrop handler like this:

[System.Security.Permissions.FileIOPermission(System.Security.Permissions.SecurityAction.Assert, Unrestricted=true)]
private void UserControl1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)

2. When setting .AllowDragDrop=true in the InitializeComponents section of the control an exception was raised during initialization of the control.  When I looked at the call stack I realized it was a message that was being received by the controls WndProc during initialization of Drag & Drop that was missing UIPermission.  To fix this I added:

[UIPermission(SecurityAction.Assert, Unrestricted=true)]
protected override void WndProc(ref Message m)
{
    base.WndProc (ref m);
}

Voila!  A working drop target in Internet Explorer. Thursday, January 20, 2005 4:26:56 PM (Pacific Standard Time, UTC-08:00)   #      Comments [0]  
 


Administration
Sign In