1
我目前正在編寫嵌入python解釋器的應用程序。這個想法是讓程序在程序中的特定事件上調用用戶指定的腳本。我管理這部分,但現在我希望腳本能夠在我的程序中調用函數。嵌入Python並將C函數添加到解釋器中
這裏是我到目前爲止的代碼:
#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();
}
謝謝!
哈哈,單個PyRun_SimpleString(「import program」);是所有需要它的工作! – monoceres 2010-05-11 15:17:40
哦,對,我也沒有認識到。 – evilpie 2010-05-11 15:20:41