1

我想通过 ctypes 加载当前的 Python DLL。我可以像 ctypes.windll.python33 一样加载它,但这取决于当前版本。我知道 sys.dllhandle 中有它的句柄,但我不知道如何转换 ctypes DLL 对象中的句柄。

4

1 回答 1

1

也许更好的 ctypes.pythonapi ?

代码来自ctypes/__init__.py

if _os.name in ("nt", "ce"):
    pythonapi = PyDLL("python dll", None, _sys.dllhandle)
elif _sys.platform == "cygwin":
    pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
else:
    pythonapi = PyDLL(None)

如果要通过句柄加载库:

hndl = sys.dllhandle
pythondll = ctypes.CDLL('python dll',  handle=hndl)

PyDLL 和 WinDll 是 CDll 的子类,不同之处在于 _func_flags_ 属性。

于 2013-07-29T17:40:33.723 回答