gpt4 book ai didi

python - 在c中嵌入Python,然后在c++程序中运行它不起作用

转载 作者:行者123 更新时间:2023-11-30 20:34:24 25 4
gpt4 key购买 nike

问题我想将我的 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).

最佳答案

我发现我所要做的就是改变它

SnowBaseConvert 类(对象):SnowBaseConvert 类:

很快就成功了 ^__^

关于python - 在c中嵌入Python,然后在c++程序中运行它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42701339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com