在 c 中嵌入 Python,然后在不工作的 c++ 程序中运行它

Posted

技术标签:

【中文标题】在 c 中嵌入 Python,然后在不工作的 c++ 程序中运行它【英文标题】:Embed Python in c and then run it in a c++ program not working 【发布时间】:2017-03-09 17:05:39 【问题描述】:

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

这里是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).

【问题讨论】:

您是否尝试过以任何方式自己调试?隔离失败的行,添加日志记录,也许使用调试器,什么?您似乎甚至不知道代码在哪一行失败。 DelphiLabelApp.exe 中出现“System.AccessViolationException”类型的第一次机会异常 DelphiLabelApp.exe 中出现“System.AccessViolationException”类型的未处理异常附加信息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。线程“Win32 线程”(0x15e4) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x1cfc) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x27b0) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x385c) 已退出,代码为 0 (0x0)。 ^ @user2357112 这是我得到的错误 所以?附加调试器并修复错误。 我用我的调试测试更新了我的问题@tambre 【参考方案1】:

我发现我所要做的就是改变这个

class SnowBaseConvert(object):class SnowBaseConvert:

然后它工作了^__^

【讨论】:

以上是关于在 c 中嵌入 Python,然后在不工作的 c++ 程序中运行它的主要内容,如果未能解决你的问题,请参考以下文章

在 C++ 中嵌入 Cython

工程脚本插件方案 - c集成Python基础篇(VC++嵌入Python)

为啥在c中嵌入python时不能导入'math'库?

如何使用 cffi 在 C 中嵌入一个返回字符串的 Python 函数?

在C语言中如何嵌入python脚本

在 C 中嵌入 Python:链接错误 - 未定义对 PyString_AsString 的引用