加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在c中嵌入python

发布时间:2020-12-16 06:57:40 所属栏目:百科 来源:网络整理
导读:我在c,borland创建了一个VCL应用程序.在我的项目中有一个文件,我在同一个方法中定义的方法中实现了嵌入式 python(我的应用程序包含一个调用嵌入式python实现的方法的按钮).当我编译时,我的构建成功.但是当我运行我的应用程序,然后单击按钮时,它会显示运行时
我在c,borland创建了一个VCL应用程序.在我的项目中有一个文件,我在同一个方法中定义的方法中实现了嵌入式 python(我的应用程序包含一个调用嵌入式python实现的方法的按钮).当我编译时,我的构建成功.但是当我运行我的应用程序,然后单击按钮时,它会显示运行时错误:“模块’PYTHON25.DLL’中地址1E091375处的访问冲突.读取地址00000004”.请帮忙.
我之前从未使用过 Python.
我的节目:

#pragma hdrstop

#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


#include "Python.h"

#include "Unit1.h"
#include "Unit2.h"
#pragma link "python25_bcpp.lib"

//---------------------------------------------------------------------------

#pragma package(smart_init)

bool callHelloWorld(int intVal)
{
    char fName[] = "Hello"; //file name
    char cFunc[] = "hello"; //method name

    char *pfName,*pcFunc;

    PyObject *pName,*pModule,*pDict,*pFunc ;

    pfName = fName;
    pcFunc = cFunc;

    Py_Initialize();

    pName = PyString_FromString(pfName);

    pModule = PyImport_Import(pName);

    pDict = PyModule_GetDict(pModule);

    pFunc = PyDict_GetItemString(pDict,pcFunc);

    if (PyCallable_Check(pFunc))
    {
        PyObject_CallObject(pFunc,NULL);
    } else
    {
        PyErr_Print();
    }


    // Py_DECREF(pModule);
    // Py_DECREF(pName);

    Py_Finalize();

    return 0;
}

解决方法

检查PyImport_Import(是搜索路径中的模块?)和PyDict_GetItemString的返回值.

如果这无助于在您的应用中添加一些跟踪消息,以查看它崩溃的位置.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读