我已经通过google进行了搜索,以及许多refence,但是我只看到一个复杂的编码示例,你会给我一个示例(简单代码),所以我可以理解。 我已经编写它,但它每次打破我运行它 下面的代码简单示例如何在C/C++中调用Python函数
#include <Python.h>
int main()
{
PyObject *pName, *pModule, *pDict, *pFun, *pValue;
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyString_FromString("C:\\Documents and Settings\\MASTER\\My Documents\\Visual Studio 2010\\Projects\\Python\\Test.py");
if(pName)printf("OK");
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFun = PyDict_GetItemString(pDict, "prinTname");
if (PyCallable_Check(pFun))
{
PyObject_CallObject(pFun, NULL);
} else
{
PyErr_Print();
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
Py_DECREF(pDict);
Py_DECREF(pFun);
// Finish the Python Interpreter
Py_Finalize();
getchar();
return 0;
}
,并有在Python.exe一些消息 第一次机会异常在0x1e00503b:0000005:访问冲突读取位置0x00000004 。 Python.exe中的0x1e00503b未处理的异常:0xC0000005:访问冲突读取位置0x00000004。 程序'[4548] Python.exe:Native'已退出,代码为0(0x0)。
至于你的问题,你有没有试过在调试器中运行你的程序?你确定所有的函数调用都成功吗? –
我已经建立了它在VS 2010和它建立成功了,我只是想它调用C/C++ 这是Python代码: DEF prinTname(): \t打印“OK玩转” –