It's can be difficult to debug Drag & Drop operations
See the question and my original answer on StackOverflowI can successfully use the provided reproduction program. It works at least on Windows 10, 11, x86 x64 in Release and Debug configuration, when ran without debugging it.
DV_E_FORMATETC (0x80040064 ) is just a standard error code that means the data object doesn't support the clipboard format that's requested. This error is common in copy/paste, drag&drop and clipboard operations.
What doesn't work at all is:
- Putting a breakpoint in the Drag & Drop handling code, for example in
void System.Runtime.InteropServices.ComTypes.IDataObject.GetData(ref FORMATETC format, out STGMEDIUM medium)
while running in Debug mode. - Raising an exception in the Drag & Drop handling code when running in Debug mode.
I don't think you can debug a .NET program (with today tools) with breakpoints or exceptions being throw when in a DoDragDrop function call.
DoDragDrop
is a big modal loop that eats most messages sent to a window. Unfortunately the .NET debugger facility or exception handler (in clr.dll) seems to also wait on this loop for some reason. So, hitting a breakpoint or handling an exception creates a hang in the program itself (and in the debugger too) that you can only break the dead lock by killing the debugged process the hard way (taskkill /im myprocess.exe /f
command line for example).
So my recommendation is to:
-
avoid putting breakpoints in these portions of code
-
wrap these functions in
try/catch
block and use some non-intrusive trace mechanism, something like this:void System.Runtime.InteropServices.ComTypes.IDataObject.GetData(ref FORMATETC format, out STGMEDIUM medium) { try { .... } catch(Exception ex) { Trace.WriteLine("GetData Error: " + ex); throw; // rethrow as is } }