gpt4 book ai didi

Python - 从 python33.dll 调用函数(访问冲突)

转载 作者:太空宇宙 更新时间:2023-11-03 15:40:01 24 4
gpt4 key购买 nike

我有这个代码:

import ctypes


lib = ctypes.WinDLL('python33.dll')
print(lib['PyRun_SimpleString'])

func = lib['PyRun_SimpleString']

func.argtypes = [ctypes.c_char_p]
func.restype = ctypes.c_int

arg = "print(':P')"
arg = arg.encode('utf-8')

func(arg)

结果:

OSError: exception: access violation reading 0x00000004

使用sublime text运行(python3.3嵌入)

最佳答案

使用PyDLL ,而不是 WinDLL。来自文档:

Instances of this class behave like CDLL instances, except that the Python GIL is not released during the function call, and after the function execution the Python error flag is checked. If the error flag is set, a Python exception is raised.

Thus, this is only useful to call Python C api functions directly.

关于Python - 从 python33.dll 调用函数(访问冲突),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42217712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com