我有这个代码:
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/