2011-10-09 82 views
4

我在C++ borland中创建了一个VCL应用程序。在我的项目中有一个文件,我在其中定义的方法中实现了嵌入式python(我的应用程序包含一个调用实现嵌入式python的方法的按钮)。当我编译时,我的构建是成功的。但是当我运行我的应用程序,然后单击该按钮时,它显示运行时错误:“模块'PYTHON25.DLL'中地址1E091375处的访问冲突。读取地址00000004”。请帮忙。 我从来没有用过Python。 我的程序:在C++中嵌入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; 
} 
+3

我似乎记得Borland C++使用不同的调用约定(fastcall?)。你的python.dll是使用相同的编译器编译的吗? – cdarke

+0

python25.dll在C:\ Windows \ SysWOW64中。我没有编译它。 –

+0

我不知道这是否会有所帮助,但我认为你错过了一些python文件。我记得在尝试在另一台机器上编译我的程序时遇到同样的错误。但在我的情况下,我正在使用一个MFC程序,并且正在使用python脚本访问我的MFC DLL。可能尝试重新安装python。 – Neophile

回答

1

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

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

0

这个工作对我来说:

只要删除Py_Finalize()

我在其他网站Py_Finalize在特定情况下,如线程的一些问题阅读。

+0

请提供资料来源? – quantum