Skip to content

Commit

Permalink
[test\test_outparam.py] Use WinDLL instead of oledll/windll
Browse files Browse the repository at this point in the history
Partially fix enthought#735
  • Loading branch information
moi15moi committed Jan 12, 2025
1 parent 6ebe947 commit 6e3762f
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions comtypes/test/test_outparam.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import unittest
from ctypes import (
HRESULT,
POINTER,
WinDLL,
byref,
c_int,
c_size_t,
c_ulong,
c_void_p,
c_wchar,
c_wchar_p,
cast,
memmove,
oledll,
sizeof,
windll,
wstring_at,
wstring_at
)
from ctypes.wintypes import DWORD, LPVOID
from unittest.mock import patch

from comtypes import COMMETHOD, GUID, IUnknown

from comtypes.GUID import _CoTaskMemFree

text_type = str

Expand All @@ -34,8 +36,19 @@ class IMalloc(IUnknown):
]


_ole32 = WinDLL("ole32")

_CoGetMalloc = _ole32.CoGetMalloc
_CoGetMalloc.argtypes = [DWORD, POINTER(POINTER(IMalloc))]
_CoGetMalloc.restype = HRESULT

SIZE_T = c_size_t
_CoTaskMemAlloc = _ole32.CoTaskMemAlloc
_CoTaskMemAlloc.argtypes = [SIZE_T]
_CoTaskMemAlloc.restype = LPVOID

malloc = POINTER(IMalloc)()
oledll.ole32.CoGetMalloc(1, byref(malloc))
_CoGetMalloc(1, byref(malloc))
assert bool(malloc)


Expand All @@ -45,14 +58,14 @@ def from_outparm(self):
result = wstring_at(self)
if not malloc.DidAlloc(self):
raise ValueError("memory was NOT allocated by CoTaskMemAlloc")
windll.ole32.CoTaskMemFree(self)
_CoTaskMemFree(self)
return result


def comstring(text, typ=c_wchar_p):
text = text_type(text)
size = (len(text) + 1) * sizeof(c_wchar)
mem = windll.ole32.CoTaskMemAlloc(size)
mem = _CoTaskMemAlloc(size)
print("malloc'd 0x%x, %d bytes" % (mem, size))
ptr = cast(mem, typ)
memmove(mem, text, size)
Expand Down

0 comments on commit 6e3762f

Please sign in to comment.