dotnet/wpf · error · ArgumentNullException
ArgumentNullException: handle
Error message
ArgumentNullException: handle
What it means
The OleDropTarget constructor wraps a Win32 window handle (HWND) that will receive OLE drop notifications. Passing IntPtr.Zero means there is no target window, so the constructor throws ArgumentNullException named "handle" immediately rather than failing later in the drop protocol.
Solutions
- Ensure the window/control is fully initialized (HWND created) before constructing OleDropTarget — e.g. wait for the Loaded event or HandleCreated.
- Obtain the handle via source.Handle on an HwndSource created from the actual window, and verify it is non-zero.
- If the handle comes from an external component, validate it is != IntPtr.Zero before constructing.
Example fix
// before
var target = new OleDropTarget(control.Handle); // may be IntPtr.Zero
// after
if (control.IsHandleCreated && control.Handle != IntPtr.Zero)
var target = new OleDropTarget(control.Handle);
else
control.HandleCreated += (s, e) => new OleDropTarget(control.Handle); Defensive patterns
Strategy: validation
Validate before calling
if (handle == IntPtr.Zero) throw new InvalidOperationException("Cannot create OleDropTarget before the window handle exists"); Type guard
static bool HasWindowHandle(IntPtr h) => h != IntPtr.Zero;
Try / catch
try { var target = new OleDropTarget(handle); }
catch (ArgumentNullException ex) when (ex.ParamName == "handle") { /* defer until handle created */ } Prevention
- Register drop targets after the window is loaded (Loaded event / HandleCreated).
- Use HwndSource.FromVisual(element).Handle rather than caching handles early.
- Always check IntPtr.Zero on handles obtained from external components.
When it happens
Trigger: Calling new OleDropTarget(IntPtr.Zero), typically when a window handle could not be obtained (e.g. HwndSource not yet created, control not initialized, or Handle property returning Zero).
Common situations: Registering a drop target during window initialization before the HWND exists; interop code that caches a handle before the window is shown; headless or disconnected window scenarios.
Related errors
- SR.OleServicesContext_ThreadMustBeSTA
- anchorLocator.Parts
- ArgumentNullException (buffer/sourceBuffer was IntPtr.Zero)
- ArgumentNullException(nameof(assemblyName))
- ArgumentNullException(nameof(assemblyNames))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/763a9a25c49187d2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DragDrop.cs:893
/// </summary>
internal class OleDropTarget : DispatcherObject, UnsafeNativeMethods.IOleDropTarget
{
//------------------------------------------------------
//
// Constructor
//
//------------------------------------------------------
#region Constructor
/// <summary>
/// OleDropTarget Constructor.
/// </summary>
public OleDropTarget(IntPtr handle)
{
if (handle == IntPtr.Zero)
{
throw new ArgumentNullException(nameof(handle));
}
_windowHandle = handle;
}
#endregion Constructor
//------------------------------------------------------
//
// IOleDropTarget Interface
//
//------------------------------------------------------
#region IOleDropTarget
/// <summary>
/// OleDragEnter - check the data object and notify DragEnter to the target element.
/// </summary>View on GitHub (pinned to 81131a70a4)