From 20baee1c25022aa753fadf336b46d35cdb424eee Mon Sep 17 00:00:00 2001 From: Yukihiro Nakadaira Date: Thu, 2 Jan 2025 11:32:06 +0900 Subject: [PATCH] Fix str conversion error of PassArrayCallback --- tests/test_winrt.py | 35 ++++++++++++++++++++++++++++++++++- win32more/_winrt.py | 2 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/tests/test_winrt.py b/tests/test_winrt.py index ba14c724..c4725d3a 100644 --- a/tests/test_winrt.py +++ b/tests/test_winrt.py @@ -4,10 +4,25 @@ from concurrent.futures import Future from pathlib import Path -from win32more import FAILED, POINTER, WINFUNCTYPE, Byte, Int32, UInt32, VoidPtr, WinError, cast, pointer +from win32more import ( + FAILED, + POINTER, + WINFUNCTYPE, + Byte, + Guid, + Int32, + UInt32, + VoidPtr, + WinError, + cast, + pointer, +) from win32more._winrt import ( + ComClass, MulticastDelegateImpl, + PassArray, ReceiveArray, + WinRT_String, _ro_get_parameterized_type_instance_iid, box_value, unbox_value, @@ -96,6 +111,24 @@ async def winrt_readlines(): lines10 = [ivector.GetAt(i) for i in range(10)] self.assertEqual(lines10, lines[0:10]) + def test_passarray_convert_hstring_to_str(self): + class IMock(IInspectable): + _classid_ = "IMock" + _iid_ = Guid("{00000000-0000-0000-0000-000000000000}") + + @winrt_commethod(6) + def f(self, p: PassArray[WinRT_String]) -> WinRT_String: ... + + class Mock(ComClass, IMock): + def f(self, p: list[str]) -> str: + return p[0] + + mock = Mock().as_(IMock) + + # winrt call: mock.f(["str"]) -> WinrtMethod([hstring]) + # winrt callback: vtbl.f([hstring]) -> Mock.f(["str"]) + self.assertEqual(mock.f(["hello"]), "hello") + def test_receivearray_param(self): inarray = [1, 2, 3] diff --git a/win32more/_winrt.py b/win32more/_winrt.py index 495f514a..3bd112a3 100644 --- a/win32more/_winrt.py +++ b/win32more/_winrt.py @@ -386,7 +386,7 @@ def __init__(self, type_, size, ptr): self._lst = [] for i in range(size): if type_ is WinRT_String: - self._lst.append(_windows_create_string(ptr[i])) + self._lst.append(_windows_get_string_raw_buffer(ptr[i])) else: self._lst.append(ptr[i])