Python embedding in c ++

I created a VCL application in C ++, borland. In my project there is a file in which I implemented the built-in python in the methods defined in the same (my application contains a button that calls the method in which the built-in python is embedded). when I compile my build is successful. but when I launch my application and click on the button, it shows a runtime error: "Access violation at address 1E091375 in the module" PYTHON25.DLL. Reading address 00000004 ". please help. I have never used Python before. my program:

#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; } 
+4
source share
2 answers

Check the return values โ€‹โ€‹of PyImport_Import (is the module in the search path?) And PyDict_GetItemString .

If this does not help put some trace messages in your application to see where they crash.

+1
source

This works for me:

Just remove Py_Finalize ()

I read on another site that Py_Finalize has some problems in specific cases, such as threads.

0
source

All Articles