Skip to content

Conversation

@B16f00t
Copy link

@B16f00t B16f00t commented Jan 25, 2025

This pull request addresses the issue with accented characters and special symbols in file paths during the drag-and-drop process.

Changes made:

  • Modified the py_drop_func to handle Unicode decoding errors.
  • Initially attempts to decode the file path using UTF-8 encoding.
  • If UTF-8 decoding fails (e.g., due to accented characters), it falls back to Windows-1252 encoding.

This fix ensures that file paths with characters like á, é, í, ó, ú, ñ, and others are handled correctly without causing errors.

This should address and close Issue #42.

Fixes issues with accented characters and special symbols by handling Unicode decoding errors.
Attempts decoding with UTF-8 first, then falls back to Windows-1252 encoding if UTF-8 fails.
@Valer100
Copy link

Valer100 commented Jan 25, 2025

For some reason it doesn't work well with some characters like ă, ș and ț. Using the same code from #42 and trying again to drag the folder named ăâîșț to the tkinter window, I'm getting this output:

['C:\\Users\\lenovo\\Desktop\\aâî??']

And normally I should get

['C:\\Users\\lenovo\\Desktop\\ăâîșț']

@Jesse205
Copy link

Jesse205 commented Nov 9, 2025

I think this fix is wrong because you can't assume that the encoding for fallback is 'Windows-1252', for example in Chinese Simplified Windows, the correct encoding is 'gbk'.

image

@Jesse205
Copy link

Jesse205 commented Nov 9, 2025

In fact, just use create_unicode_buffer and DragQueryFileW, so that the string obtained is unicode.

diff --git a/pywinstyles/py_win_style.py b/pywinstyles/py_win_style.py
index fe41ddf..8056ff6 100644
--- a/pywinstyles/py_win_style.py
+++ b/pywinstyles/py_win_style.py
@@ -5,6 +5,7 @@ Version: 1.8
 """
 
 from __future__ import annotations
+from ctypes import create_unicode_buffer
 from typing import Any, Union, Callable
 
 try:
@@ -191,8 +192,8 @@ class apply_dnd():
         prototype = WINFUNCTYPE(typ, typ, typ, typ, typ)
         WM_DROP_FILES = 0x233
         GWL_WND_PROC = -4
-        create_buffer = c_buffer
-        func_DragQueryFile = (windll.shell32.DragQueryFile)
+        create_buffer = create_unicode_buffer
+        func_DragQueryFile = (windll.shell32.DragQueryFileW)
 
         def py_drop_func(hwnd, msg, wp, lp):
             global files
@@ -202,7 +203,7 @@ class apply_dnd():
                 files = []
                 for i in range(count):
                     func_DragQueryFile(typ(wp), i, file_buffer, sizeof(file_buffer))
-                    drop_name = file_buffer.value.decode("utf-8")
+                    drop_name = file_buffer.value
                     files.append(drop_name)
                 func(files)
                 windll.shell32.DragFinish(typ(wp))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants