From a40b61b232d2032416777ab6f509bad534965252 Mon Sep 17 00:00:00 2001 From: David Schranz Date: Wed, 27 Nov 2024 09:52:46 +0100 Subject: [PATCH 1/4] Add VT_HRESULT in _ctype_to_vartype --- comtypes/automation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/comtypes/automation.py b/comtypes/automation.py index 61797dd8..9f1df3e0 100644 --- a/comtypes/automation.py +++ b/comtypes/automation.py @@ -956,6 +956,7 @@ def Invoke(self, dispid: int, *args: Any, **kw: Any) -> Any: c_ulonglong: VT_UI8, VARIANT_BOOL: VT_BOOL, BSTR: VT_BSTR, + HRESULT: VT_HRESULT, VARIANT: VT_VARIANT, # SAFEARRAY(VARIANT *) # From d6c99ff9cfc471f17436ff6a8ec5d7fbb135e917 Mon Sep 17 00:00:00 2001 From: David Schranz Date: Wed, 27 Nov 2024 09:53:53 +0100 Subject: [PATCH 2/4] Add exception when attempting to call the _midlSAFEARRAY(HRESULT).create --- comtypes/safearray.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/comtypes/safearray.py b/comtypes/safearray.py index 70e7c3ab..d939ffdb 100644 --- a/comtypes/safearray.py +++ b/comtypes/safearray.py @@ -78,6 +78,7 @@ def _make_safearray_type(itemtype): VT_UNKNOWN, IDispatch, VT_DISPATCH, + VT_HRESULT, ) meta = type(_safearray.tagSAFEARRAY) @@ -123,6 +124,15 @@ def create(cls, value, extra=extra): one-dimensional arrays. To create multidimensional arrys, numpy arrays must be passed. """ + if cls._vartype_ == VT_HRESULT: + raise TypeError( + # There are COM type libraries that define the + # `_midlSAFEARRAY(HRESULT)` type; however, creating `HRESULT` + # safearray pointer instance does not work. + # See also: https://github.com/enthought/comtypes/issues/668 + "Cannot create SAFEARRAY type VT_HRESULT." + ) + if comtypes.npsupport.isndarray(value): return cls.create_from_ndarray(value, extra) From 6ad5737f0505c87f0a7cbeef393036bfc189ae2b Mon Sep 17 00:00:00 2001 From: David Schranz Date: Wed, 27 Nov 2024 13:35:39 +0100 Subject: [PATCH 3/4] Fix formating issue --- comtypes/safearray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comtypes/safearray.py b/comtypes/safearray.py index d939ffdb..6330d552 100644 --- a/comtypes/safearray.py +++ b/comtypes/safearray.py @@ -131,7 +131,7 @@ def create(cls, value, extra=extra): # safearray pointer instance does not work. # See also: https://github.com/enthought/comtypes/issues/668 "Cannot create SAFEARRAY type VT_HRESULT." - ) + ) if comtypes.npsupport.isndarray(value): return cls.create_from_ndarray(value, extra) From 65eb423cf808b8690df90515863df9f81e3d4835 Mon Sep 17 00:00:00 2001 From: David Schranz Date: Wed, 27 Nov 2024 13:36:40 +0100 Subject: [PATCH 4/4] Add test code --- comtypes/test/test_midl_safearray_create.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/comtypes/test/test_midl_safearray_create.py b/comtypes/test/test_midl_safearray_create.py index d632b5e7..594e0a57 100644 --- a/comtypes/test/test_midl_safearray_create.py +++ b/comtypes/test/test_midl_safearray_create.py @@ -1,6 +1,6 @@ # coding: utf-8 -from ctypes import c_int, pointer, POINTER +from ctypes import c_int, pointer, HRESULT, POINTER import unittest import comtypes @@ -82,6 +82,14 @@ def test_record(self): self.assertEqual(unpacked.answer, 42) self.assertEqual(unpacked.needs_clarification, True) + def test_HRESULT(self): + hr = HRESULT(1) + sa_type = comtypes.safearray._midlSAFEARRAY(HRESULT) + with self.assertRaises(TypeError): + sa_type.create([hr], extra=None) + with self.assertRaises(TypeError): + sa_type.create([hr]) + def test_ctype(self): extra = None cdata = c_int(1)