在c中嵌入Python,然后在c++程序中运行它

2024-09-28 21:41:48 发布

您现在位置:Python中文网/ 问答频道 /正文

问题:我想将python模块嵌入到c代码中,然后从c++应用程序运行它。由于某些原因,我不能让它工作,每次我运行它只是崩溃。我设置了PYTHONPATH并加载了调试python符号,并为c++编译了PYTHONPATH,即使没有错误,我的应用程序也不想启动。任何帮助都将不胜感激!!!你知道吗

下面是python文件tobase34.py

class SnowBaseConvert(object):

  #def __init__(self, innitvar, basevar, convertvar):
  #  self.innitvar = innitvar
  #  self.basevar = basevar
  #  self.convertvar = convertvar

  def main(self, innitvar, basevar, convertvar):
    SY2VA = {'0': 0,
         '1': 1,
         '2': 2,
         '3': 3,
         '4': 4,
         '5': 5,
         '6': 6,
         '7': 7,
         '8': 8,
         '9': 9,
         'A': 10,
         'B': 11,
         'C': 12,
         'D': 13,
         'E': 14,
         'F': 15,
         'G': 16,
         'H': 17,
         'I': 18,
         'J': 19,
         'K': 20,
         'L': 21,
         'M': 22,
         'N': 23,
         'O': 24,
         'P': 25,
         'Q': 26,
         'R': 27,
         'S': 28,
         'T': 29,
         'U': 30,
         'V': 31,
         'W': 32,
         'X': 33,
         'Y': 34,
         'Z': 35,
         'a': 36,
         'b': 37,
         'c': 38,
         'd': 39,
         'e': 40,
         'f': 41,
         'g': 42,
         'h': 43,
         'i': 44,
         'j': 45,
         'k': 46,
         'l': 47,
         'm': 48,
         'n': 49,
         'o': 50,
         'p': 51,
         'q': 52,
         'r': 53,
         's': 54,
         't': 55,
         'u': 56,
         'v': 57,
         'w': 58,
         'x': 59,
         'y': 60,
         'z': 61,
         '!': 62,
         '"': 63,
         '#': 64,
         '$': 65,
         '%': 66,
         '&': 67,
         "'": 68,
         '(': 69,
         ')': 70,
         '*': 71,
         '+': 72,
         ',': 73,
         '-': 74,
         '.': 75,
         '/': 76,
         ':': 77,
         ';': 78,
         '<': 79,
         '=': 80,
         '>': 81,
         '?': 82,
         '@': 83,
         '[': 84,
         '\\': 85,
         ']': 86,
         '^': 87,
         '_': 88,
         '`': 89,
         '{': 90,
         '|': 91,
         '}': 92,
         '~': 93}
    integer = 0
    for character in innitvar:
        assert character in SY2VA, 'Found unknown character!'
        value = SY2VA[character]
        assert value < basevar, 'Found digit outside base!'
        integer *= basevar
        integer += value

    VA2SY = dict(map(reversed, SY2VA.items()))


    array = []
    while integer:
        integer, value = divmod(integer, convertvar)
        array.append(VA2SY[value])
    answer = ''.join(reversed(array))

    return answer

#SnowBaseConvert().main('056866536', 10, 34)

这是我的c文件base34conv.c

#include "base34conv.h"

 char * convertbase34(){

    //Py_SetPythonHome("C:\\Python27");

    Py_Initialize();

    PyObject* module = PyImport_ImportModule("tobase34");
    assert(module != NULL);

    PyObject* klass = PyObject_GetAttrString(module, "SnowBaseConvert");
    assert(klass != NULL);

    PyObject* instance = PyInstance_New(klass, NULL, NULL);
    assert(instance != NULL);

    PyObject* result = PyObject_CallMethod(instance, "main", "(sii)", "056866536", 10, 34); 
    assert(result != NULL);

    Py_Finalize();


    return PyString_AsString(result);

}

这是我的实现文件base34conv.h

#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#pragma comment(lib, "C:\\Python27\\libs\\python27.lib")


#ifdef _DEBUG
  #undef _DEBUG
  #include "C:\\Python27\\include\\Python.h"
  #define _DEBUG
#else
  #include "C:\\Python27\\include\\Python.h"
#endif



#ifdef __cplusplus
extern "C" {
#endif

    char * convertbase34();

#ifdef __cplusplus
}
#endif

最后是我的c++文件,它是一个win窗体应用程序

#include "base34conv.h"

namespace TestApp{

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::Security;
    using namespace System::Diagnostics;
    using namespace System::Xml;
    using namespace System::Management;
    using namespace System::ServiceProcess;
    using namespace System::Timers;
    using namespace std;
    using namespace System::Text;
    using namespace System::Threading;
public ref class Main : public System::Windows::Forms::Form
{
public:
    Main(void)
    {
        InitializeComponent();


        //
        //TODO: Add the constructor code here
        //
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Main()
    {
        if (components)
        {
            delete components;
        }
    }



private: System::Void TestForm_Load(System::Object^  sender, System::EventArgs^  e) 
      {




     String^ base34conversion;

     base34conversion = gcnew String(convertbase34());

     Console::Write(base34conversion);

     test_box->Text = base34conversion->ToString();




  }
};
}

调试吐出

Program: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
File: base34conv.c
Line: 16

Expression: instance != NULL

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts

(Press Retry to debug the application - JIT must be enabled)'DelphiLabelApp.exe': Loaded 'C:\Windows\SysWOW64\clbcatq.dll', Symbols loaded (source information stripped).
Assertion failed!

Program: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
File: base34conv.c
Line: 19

Expression: result != NULL

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts

(Press Retry to debug the application - JIT must be enabled)First-chance exception at 0x1d0eb1f9 (python27.dll) in testapp.exe: 0xC0000005: Access violation reading location 0x00000004.
A first chance exception of type 'System.AccessViolationException' occurred in testapp.exe
An unhandled exception of type 'System.AccessViolationException' occurred in testapp.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The thread 'Win32 Thread' (0x47d4) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x44b4) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x2d18) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x1ad4) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x2c44) has exited with code 0 (0x0).

Tags: inselfincludecodeintegerassertnamespacesystem