gpt4 book ai didi

c++ - 在 C++ 中嵌入 Python

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:19:38 26 4
gpt4 key购买 nike

我在 borland 的 c++ 中创建了一个 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

如果这没有帮助,请在您的应用中放置一些跟踪消息以查看它崩溃的位置。

关于c++ - 在 C++ 中嵌入 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7705462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com