我目前正在编写一个嵌入 python 解释器的应用程序。这个想法是让程序在程序中的某些事件上调用用户指定的脚本。我管理了这部分,但现在我希望脚本能够在我的程序中调用函数。
到目前为止,这是我的代码:
#include "python.h"
static PyObject* myTest(PyObject* self,PyObject *args)
{
return Py_BuildValue("s","123456789");
}
static PyMethodDef myMethods[] = {{"myTest",myTest},{NULL,NULL}};
int main()
{
Py_Initialize();
Py_InitModule("PROGRAM",myMethods);
PyRun_SimpleString("print PROGRAM.myTest()");
Py_Finalize();
}
谢谢!