Skip to content

Commit 7b31899

Browse files
authored
Allow the definition of the _midlSAFEARRAY(HRESULT) type. (#670)
* Add VT_HRESULT in _ctype_to_vartype * Add exception when attempting to call the _midlSAFEARRAY(HRESULT).create * Fix formating issue * Add test code
1 parent fb15f91 commit 7b31899

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

comtypes/automation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ def Invoke(self, dispid: int, *args: Any, **kw: Any) -> Any:
956956
c_ulonglong: VT_UI8,
957957
VARIANT_BOOL: VT_BOOL,
958958
BSTR: VT_BSTR,
959+
HRESULT: VT_HRESULT,
959960
VARIANT: VT_VARIANT,
960961
# SAFEARRAY(VARIANT *)
961962
#

comtypes/safearray.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def _make_safearray_type(itemtype):
7878
VT_UNKNOWN,
7979
IDispatch,
8080
VT_DISPATCH,
81+
VT_HRESULT,
8182
)
8283

8384
meta = type(_safearray.tagSAFEARRAY)
@@ -123,6 +124,15 @@ def create(cls, value, extra=extra):
123124
one-dimensional arrays. To create multidimensional arrys,
124125
numpy arrays must be passed.
125126
"""
127+
if cls._vartype_ == VT_HRESULT:
128+
raise TypeError(
129+
# There are COM type libraries that define the
130+
# `_midlSAFEARRAY(HRESULT)` type; however, creating `HRESULT`
131+
# safearray pointer instance does not work.
132+
# See also: https://github.com/enthought/comtypes/issues/668
133+
"Cannot create SAFEARRAY type VT_HRESULT."
134+
)
135+
126136
if comtypes.npsupport.isndarray(value):
127137
return cls.create_from_ndarray(value, extra)
128138

comtypes/test/test_midl_safearray_create.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding: utf-8
22

3-
from ctypes import c_int, pointer, POINTER
3+
from ctypes import c_int, pointer, HRESULT, POINTER
44
import unittest
55

66
import comtypes
@@ -82,6 +82,14 @@ def test_record(self):
8282
self.assertEqual(unpacked.answer, 42)
8383
self.assertEqual(unpacked.needs_clarification, True)
8484

85+
def test_HRESULT(self):
86+
hr = HRESULT(1)
87+
sa_type = comtypes.safearray._midlSAFEARRAY(HRESULT)
88+
with self.assertRaises(TypeError):
89+
sa_type.create([hr], extra=None)
90+
with self.assertRaises(TypeError):
91+
sa_type.create([hr])
92+
8593
def test_ctype(self):
8694
extra = None
8795
cdata = c_int(1)

0 commit comments

Comments
 (0)