在C/C++中调用Python函数的简单示例

2024-04-19 10:05:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经搜索过google和许多参考资料,但我只看到一个复杂的编码示例,你能给我一个示例(简单的代码)让我能理解。 我已经编码了,但每次我运行它时它都会崩溃 这是密码

#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处的第一次机会异常:0xc000005:访问冲突读取位置0x00000004。 Python.exe中0x1e00503b处未处理的异常:0xc000005:访问冲突读取位置0x00000004。 程序“[4548]Python.exe:Native”已退出,代码为0(0x0)。


Tags: the代码py示例编码objectexepyobject
2条回答

您很可能还没有安装python开发工具,也就是说,python.h永远也找不到。找到python.h并查找编译错误,如果有,则重新发送。

编辑:这是旧信息。include应该和将include目录添加到include目录一样简单,请参见:How do I get Visual Express 2010 to find my python.h header file?

这是我问的一个问题的代码。这应该是你想要的。

参数: argv[1]包含指向.py文件的路径 argv[2]包含要调用的函数的名称

int wmain(int argc, wchar_t** argv)
{
    PyObject* py_imp_str;
    PyObject* py_imp_handle;
    PyObject* py_imp_dict; //borrowed
    PyObject* py_imp_load_source; //borrowed
    PyObject* py_dir; //stolen
    PyObject* py_lib_name; //stolen
    PyObject* py_args_tuple;
    PyObject* py_lib_mod;
    PyObject* py_func;
    PyObject* py_ret;

    Py_Initialize();

    //import our python script using the imp library (the normal import doesn't allow you to grab py files in random directories)
    py_dir = PyUnicode_FromWideChar(argv[1], wcslen(argv[1]));
    py_imp_str = PyString_FromString("imp");
    py_imp_handle = PyImport_Import(py_imp_str); //normal python import for imp
    py_imp_dict = PyModule_GetDict(py_imp_handle); //borrowed
    py_imp_load_source = PyDict_GetItemString(py_imp_dict, "load_source"); //borrowed
    py_lib_name = PyUnicode_FromWideChar(argv[2], wcslen(argv[2]));

    //setup args for imp.load_source
    py_args_tuple = PyTuple_New(2);
    PyTuple_SetItem(py_args_tuple, 0, py_lib_name); //stolen
    PyTuple_SetItem(py_args_tuple, 1, py_dir); //stolen

    //call imp.load_source
    py_lib_mod = PyObject_CallObject(py_imp_load_source, py_args_tuple);
    py_lib_mod_dict = PyModule_GetDict(py_lib_mod); //borrowed

    //get function object
    py_func = PyDict_GetItem(py_lib_mod_dict, py_lib_name);
    //call function object
    py_ret = PyObject_CallFunction(py_func, NULL);

    if (py_ret)
    {
        printf("PyObject_CallFunction from wmain was successful!\n");
        Py_DECREF(py_ret);
    }
    else
        printf("PyObject_CallFunction from wmain failed!\n");

    Py_DECREF(py_imp_str);
    Py_DECREF(py_imp_handle);
    Py_DECREF(py_args_tuple);
    Py_DECREF(py_lib_mod);

    Py_Finalize();

    fflush(stderr);
    fflush(stdout);
    return 0;
}

如果您还没有找到它,下面的链接描述了Python的C接口。非常方便: http://docs.python.org/2/c-api/index.html

相关问题 更多 >