2012-06-15 60 views
2

當我嘗試使用下面的代碼運行外部Python代碼時,我有這個Visual C++代碼以及嵌入其中的Python,當我嘗試使用以下代碼運行外部Python代碼時,我在調試模式下得到錯誤:從Visual C++代碼(嵌入式Python)運行Python

Unhandled exception at 0x77cf15de in CMLAir.exe (my code): 
access violation writing location 0x00000014. 

當通過C++代碼調用PyRun_File函數時發生錯誤。
這裏是C++函數:

void CRailtestDoc::OnPreprocessorRunscript() 
    { 

    #ifdef USE_PYTHON 

//for now, pull up Open dialog for user to specify script 
CString strPythonScriptPath; 
CString strTitle = "Run Python Script"; 
CFileDialog dlg(TRUE, ".py", NULL); 
dlg.m_ofn.Flags |= OFN_PATHMUSTEXIST; 
dlg.m_ofn.lpstrTitle = (LPCTSTR)strTitle; 
if (dlg.DoModal() == IDOK) 
{ 
    strPythonScriptPath = dlg.GetFileName(); 
} 
else 
{ 
    return; 
} 

FILE* fp; 
fp = fopen((LPCSTR)strPythonScriptPath, "r+"); 
if (fp == NULL) 
{ 
    CString strErrMsg; 
    strErrMsg.Format("troble opening python file: %s", (LPCSTR)strPythonScriptPath); 
    AfxMessageBox(strErrMsg); 
    return; 
} 

// 
// TODO: we need to make sure that we don't call Py_Initialize more than once 
// see Python/C API Reference Manual section 1.4 
// 
m_bRunningScript = TRUE; 
Py_Initialize(); 

PycString_IMPORT; //Macro for importing cStringIO objects 

//start up our own modules? Is this needed here? 
initcml(); 
initresults(); 

PyObject* localDict; 
PyObject* mainModule; 
PyObject* mainModuleDict; 
mainModule = PyImport_AddModule("__main__"); 
//mainModule = PyImport_AddModule("__builtins__"); 

if(mainModule==NULL) 
{ 
    AfxMessageBox("Problems running script"); 
    m_bRunningScript = FALSE; 
    return; 
} 
mainModuleDict = PyModule_GetDict(mainModule); 
localDict = PyDict_New(); 

//Now run the selected script 
PyObject* tempPyResult = 
PyRun_File(fp, strPythonScriptPath, Py_file_input, mainModuleDict, mainModuleDict); 
//<=== where the code exit with unhandled exception at 0x77cf15de 
UNREFERENCED_PARAMETER(tempPyResult); 

// See if an exception was raised and not handled. 
// If so, return traceback to user as MsgBox 

} 

這裏是外部的Python腳本我想從C++代碼運行:

import cml, results 

    def main(): 
    baseCase = "C:\\Program Files\\CMLAir32\\Examples\\Quick4_Example.cml" 
    outFile = "c:\\output.txt" 
    f = open(outFile, "w") 

    #open our .cml case 
    if cml.OpenCase(baseCase) == 0: 
    return 

    #set initial mesh size 
    meshSize = 177 
    cml.SetGridSizeX(meshSize) 
    cml.SetGridSizeY(meshSize) 
    cml.SaveCaseNoWarn() 
    while meshSize < 400: 
    #run it 
    cml.RunCaseNoWarn() 
    #get results 
    res = results.Results() 
    res.GetResults() 
    if res.HasResults() == 0: 
     cml.MsgBox("error getting results, aborting") 
     return 
    #pull out minimum fly height 
    singleResult = res[0] 
    fh = singleResult.minFH 
    #output current mesh size and minimum fly height 
    dataStr = str(meshSize) + ' ' + str(fh) 
    f.write(dataStr) 
    #increment mesh size 
    meshSize += 16 
    cml.SetGridSizeX(meshSize) 
    cml.SetGridSizeY(meshSize) 
    cml.SaveCaseNoWarn() 

    main() 

爲什麼PyRun_File功能給出錯誤?

我對C++代碼中嵌入Python知之甚少,因此我非常欣賞這裏的一些指針。請記住,我對Python相對來說比較陌生;我的大部分編程經驗都在Visual C++中。在這種情況下將兩者融合在一起的最佳方式是什麼?

回答

2

錯誤是因爲誤解或C的fp ++和fp Python版本之間可能混淆,所以,我所做的是我註釋掉fp線,而不是我用:

PyObject* PyFileObject; PyFileObject = PyFile_FromString(strPythonScriptPath, "r"); 

和工作剛剛好!