簡體   English   中英

在c ++中嵌入python

[英]embedding python in c++

我用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的返回值。

如果這無助於在您的應用中添加一些跟蹤消息,以查看它崩潰的位置。

這對我有用:

只需刪除Py_Finalize()

我在另一個網站上讀到Py_Finalize在特定情況下遇到一些問題,比如線程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM