Subject: bug#74312: 31.0.50; Cygw32 build break

From: Cecilio Pardo <[email-masked]>
Date: Thu, 14 Nov 2024 09:55:56 +0100

On 13/11/2024 16:55, Kazuhiro Ito wrote:
>>> Both Emacsen may fail to handle dropped files if I do drag-n-drop
>>> files very quickly.  I confirmed Microsoft Photo application on
>>> Windows 11.  And, once Emacs failed to handle drop event, it keeps
>>> ignoring dropping files or text event.
>>
>> I will look into this.
> 
> As far as I tested, drag-n-drop from Microsoft Photo worked only the
> first time.  Second operation was ignored.  Quick operation may fail
> even at the first time.  Drag-n-drop from explorer worked expectedly.

This should fix the Photo application problem.

I didn't expect ref counting to be needed for this, my bad.

diff --git a/src/w32fns.c b/src/w32fns.c
index 1bd3d5099e2..e2455b9271e 100644
--- a/src/w32fns.c
+++ b/src/w32fns.c
@@ -2562,6 +2562,7 @@ w32_process_dnd_data (int format, void *hGlobal)
    /* i_drop_target must be the first member.  */
    IDropTarget i_drop_target;
    HWND hwnd;
+  int ref_count;
  };

  static HRESULT STDMETHODCALLTYPE
@@ -2573,13 +2574,16 @@ w32_drop_target_QueryInterface (IDropTarget *t, 
REFIID ri, void **r)
  static ULONG STDMETHODCALLTYPE
  w32_drop_target_AddRef (IDropTarget *This)
  {
-  return 1;
+  struct w32_drop_target *target = (struct w32_drop_target *) This;
+  return ++target->ref_count;
  }

  static ULONG STDMETHODCALLTYPE
  w32_drop_target_Release (IDropTarget *This)
  {
    struct w32_drop_target *target = (struct w32_drop_target *) This;
+  if (--target->ref_count > 0)
+    return target->ref_count;
    free (target->i_drop_target.lpVtbl);
    free (target);
    return 0;
@@ -2770,6 +2774,7 @@ w32_createwindow (struct frame *f, int *coords)
  	  if (vtbl != NULL)
  	    {
  	      drop_target->hwnd = hwnd;
+	      drop_target->ref_count = 0;
  	      drop_target->i_drop_target.lpVtbl = vtbl;
  	      vtbl->QueryInterface = w32_drop_target_QueryInterface;
  	      vtbl->AddRef = w32_drop_target_AddRef;