Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses #63 by unpacking VT_ARRAYs of VT_RECORDs #104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions comtypes/_safearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ def SafeArrayGetVartype(pa):
_SafeArrayGetVartype(pa, result)
return result.value

def SafeArrayGetRecordInfo(pa):
# Defined here to avoid a circular import
from comtypes.typeinfo import IRecordInfo

_SafeArrayGetRecordInfo = _oleaut32.SafeArrayGetRecordInfo
_SafeArrayGetRecordInfo.restype = HRESULT
_SafeArrayGetRecordInfo.argtypes = [POINTER(SAFEARRAY), POINTER(POINTER(IRecordInfo))]

result = POINTER(IRecordInfo)()
_SafeArrayGetRecordInfo(pa, byref(result))
return result.value

SafeArrayGetElement = _oleaut32.SafeArrayGetElement
SafeArrayGetElement.restype = HRESULT
SafeArrayGetElement.argtypes = [POINTER(SAFEARRAY), POINTER(LONG), c_void_p]
Expand Down
27 changes: 25 additions & 2 deletions comtypes/automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,31 @@ def _get_value(self, dynamic=False):

return value
elif self.vt & VT_ARRAY:
typ = _vartype_to_ctype[self.vt & ~VT_ARRAY]
return cast(self._.pparray, _midlSAFEARRAY(typ)).unpack()
try:
typ = _vartype_to_ctype[self.vt & ~VT_ARRAY]
return cast(self._.pparray, _midlSAFEARRAY(typ)).unpack()
except KeyError:
pass

if self.vt & VT_RECORD:
from comtypes.client import GetModule
from comtypes.typeinfo import IRecordInfo

# Get generic safearray and its IRecordInfo
ri0 = cast(self._.pparray, POINTER(_safearray.SAFEARRAY))
ri1 = _safearray.SafeArrayGetRecordInfo(ri0)
# QUESTION: Do we need to add a ref ?
ri2 = ri1.QueryInterface(IRecordInfo)

# Now containing typelib and through it the right class type
mod = GetModule(ri2.GetTypeInfo().GetContainingTypeLib()[0])
ricls = getattr(mod, ri2.GetName())

# Cast the array to the right safearray(type), unpack -> return
return cast(self._.pparray, _midlSAFEARRAY(ricls)).unpack()

# Else we cannot unpack the array
raise NotImplementedError("typecode %d = 0x%x)" % (vt, vt))
else:
raise NotImplementedError("typecode %d = 0x%x)" % (vt, vt))

Expand Down
9 changes: 7 additions & 2 deletions comtypes/safearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,13 @@ def _get_elements_raw(self, num_elements):
return ptr[:num_elements]

def keep_safearray(v):
v.__keepref = self
return v
# Simply keeping a reference to self does not keep the
# internal addresses of BSTR safe and strings will be
# overwritten. A copy of the bytes solves the problem
v1 = v.__class__()
memmove(byref(v1), byref(v), sizeof(v))
return v1

return [keep_safearray(x) for x in ptr[:num_elements]]
finally:
_safearray.SafeArrayUnaccessData(self)
Expand Down