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 *) # diff --git a/comtypes/safearray.py b/comtypes/safearray.py index 70e7c3ab..6330d552 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) 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)