【发布时间】:2012-10-28 13:33:16
【问题描述】:
我已经通过谷歌搜索了很多参考资料,但我只看到了一个复杂的编码示例,你能给我一个例子(简单代码)以便我理解。 我已经编写了代码,但每次运行它都会中断 这是代码
#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 处的第一次机会异常:0xC0000005:访问冲突读取位置 0x00000004。 Python.exe 中 0x1e00503b 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000004。 程序“[4548] Python.exe: Native”已退出,代码为 0 (0x0)。
【问题讨论】:
-
至于你的问题,你试过在调试器中运行你的程序吗?你确定所有的函数调用都成功了吗?
-
我已经在 VS 2010 中构建了它并且构建成功了,我只想在 C/C++ 中调用它这是 python 代码:def prinTname(): print "OK Fun"