2017-03-09 12 views
-2

問題我想將我的python模塊嵌入到我的c代碼中,然後從那裏我想從C++應用程序運行它。出於某種原因,我無法在每次運行它時崩潰。我設置我的PYTHONPATH以及加載在調試python符號和編譯爲c + +即使我沒有得到任何錯誤,我的應用程序只是不想開始。任何幫助都將不勝感激!將Python嵌入到c中,然後在一個不工作的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++文件它的勝利形式應用程序

#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). 
+1

你有沒有試圖以任何方式自己調試它?隔離失敗的行,添加日誌記錄,可能使用調試器,什麼?你甚至不知道代碼在哪一行失敗。 – user2357112

+0

DelphiLabelApp.exe中發生類型'System.AccessViolationException'的第一次機會異常 DelphiLabelApp.exe中發生未處理的'System.AccessViolationException'類型異常 附加信息:試圖讀取或寫入受保護的內存。這通常表明其他內存已損壞。 線程'Win32線程'(0x15e4)已退出,代碼爲0(0x0)。 線程'Win32 Thread'(0x1cfc)已退出,代碼爲0(0x0)。 線程'Win32 Thread'(0x27b0)已退出,代碼爲0(0x0)。 線程'Win32線程'(0x385c)已退出,代碼爲0(0x0)。 – sNOWsYSTEM

+0

^@ user2357112這是我得到的錯誤 – sNOWsYSTEM

回答

0

我想通了所有我需要做的就是從改變這種

class SnowBaseConvert(object):class SnowBaseConvert:

和急它的工作^ __^

相關問題