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

[test\test_outparam.py] Use WinDLL/OleDLL instead of windll/oledll #765

Merged
merged 1 commit into from
Jan 26, 2025
Merged
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
28 changes: 22 additions & 6 deletions comtypes/test/test_outparam.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import unittest
from ctypes import (
HRESULT,
POINTER,
OleDLL,
WinDLL,
byref,
c_int,
c_size_t,
c_ulong,
c_void_p,
c_wchar,
c_wchar_p,
cast,
memmove,
oledll,
sizeof,
windll,
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 +37,21 @@ class IMalloc(IUnknown):
]


_ole32 = OleDLL("ole32")

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

_ole32_nohresult = WinDLL("ole32")

SIZE_T = c_size_t
_CoTaskMemAlloc = _ole32_nohresult.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 +61,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